Plotting an implicit solution obtained by differential equation in MATLAB - matlab

syms y(x)
ode=diff(y,x)*(2+x-3*y^2)==(6*x^2-y+3);
cond=y(0)==3;
ySol(x)=dsolve(ode,cond);
fplot(ySol(x));
Hello, when I execute this code, the graph is like:
But it should be like:
So the right part of the graph is not plotted by MATLAB with this code.
I tried a different code to plot the implicit function graph but it gives an error:
syms y(x)
ode=diff(y,x)*(2+x-3*y^2)==(6*x^2-y+3);
cond=y(0)==3;
s=dsolve(ode,'Implicit',true,cond);
fimplicit(ySol(x));

Related

My code for Taylor series graphs cos(2x) instead of cos(x) in Matlab

I'm writing a function that calculates the Taylor series of any Function.
syms x
y=cos(x);
y0=0;
a=0;
for i=0:25
diff(y,i); %%Gives the derivative formula
y0=y0+diff(y,i)*((x-a)^i)/factorial(i); %%sums every new element of the series
end
x=0:0.1:2*pi;
res = subs(y0,x);
plot(x,res,x,cos(x))
This is the Matlab code.
My problem is that it graphs cos(2x) instead of cos(x), similarly it graphs ln(2x) instead of ln(x) and so on.
I have checked the factorials and they appear to be correct.
What could be the problem, have I messed up the series or have I made a Matlab mistake?
You are constructing the Taylor polynomial around the point x with incrementx-a, that is, you are computing an approximation for
f(x+(x-a))=f(2*x-a)
Now as a=0, this means that as observed, you get f(2*x).
You would need to evaluate the derivatives at a to get the correct coefficients.
y0=y0+subs(diff(y,i),a)*((x-a)^i)/factorial(i); %%sums every new element of the series

Matlab surface plotting error for a specific function only

I have a small matlab script -
rho=1;
phi=0.5;
a=pi/4;
[x,y]=meshgrid(-1:1:1);
syms u;
Ex=abs(int(sqrt(cos(u))*exp(1i*rho*(cos(u-x.^2))),-a,a));
surf(x,y,Ex);
This throws the error
Error using matlab.graphics.chart.primitive.Surface/set
Invalid parameter/value pair arguments.
But there shouldn't be as Ex is a 3x3 matrix, so is the grid. Surface plotting should proceed smoothly.
But if I define a function that doesn't use the int() function, for example -
z=x.^2+y.^2;
surf(x,y,z);
Then there is no error.
Does it has something to do with syms variables?
Any help would be appreciated.

Bodeplot of a differential equation using a fourier transform in MATLAB

I have a second order differential equation which I want to transform to the frequency domain using a fourier transform from which I can create a bode plot using matlab.
I have transformed the equation by hand already, but I can't seem to find documentation on how to get the bode plot going. The bodeplot accepts a laplace transform functions from what I have read.
So my question is, how can I define a function in the frequency domain in order to create a bode plot with said function?
Another thing I am curious about is how I can take a differential equation in matlab and transform it into the frequency domain using fourier?
The differential equation is attached below.
Use the Laplace transform to get the transfer function from the differential equation:
Y(s)/X(s) = 1/(10*s^2+0.1*s+1)
In MATLAB, this is defined easily (you need the Control System Toolbox) with the tf function:
H = tf(1,[10 0.1 1]);
and then you can use the bode function to get the bode plot:
bode(H)

Implementing Euler's method for solving differential equations

This code is to find the Euler's method on MATLAB
function [x,y]=euler_forward(f,xinit,yinit,xfinal,n)
h=(xfinal-xinit)/n;
% Initialization of x and y as column vectors
x=[xinit zeros(1,n)]; y=[yinit zeros(1,n)];
% Calculation of x and y
for i=1:n
x(i+1)=x(i)+h;
y(i+1)=y(i)+h*f(x(i),y(i));
end
end`
'f=#(x,y) (1+2*x)*sqrt(y);
% Calculate exact solution
g=#(x,y) (1+2*x)*sqrt(y);
xe=[0:0.01:1];
ye=g(xe);
[x1,y1]=euler_forward(f,0,1,1,4);
% Plot
plot(xe,ye,'k-',x1,y1,'k-.')
xlabel('x')
ylabel('y')
legend('Analytical','Forward')
% Estimate errors
error1=['Forward error: ' num2str(-100*(ye(end)-y1(end))/ye(end)) '%'];
error={error1}
So I have this so far for the problem and the it gives an error saying y is not defined. What do I do?
The problem is here:
% Calculate exact solution
g=#(x,y) (1+2*x)*sqrt(y);
Exact solution is a function of one variable, x. Presumably, you were supposed to find it with paper and pencil (or with Wolfram Alpha, etc): it is
g=#(x) .25*(x.^2+x+2).^2
Then the code works as expected, producing this neat chart: as is typical for Euler's method, the numerical solution lags behind the exact one.

plotting symfun and a self created function on same graph matlab

i am trying to plot two function in matlab, the first one is of kinf symfun:
p = symfun(0, [m]);
p(m) = p(m)+Ck(k-3)*exp(m*(k-3)*complex(0, 2*pi/25));
here Ck is another symfun and k is a variable i pre-defined.
i want to plot it in the same graph with a function i created using the function mode:
function [x1] = xt_otot_q3( t)...
i cant make the xt_otot_q3 function a symfun because it involves if statements.
- i tried to create 2 vectors sampling the two functions and plotting them together with the plot function but for some reason the 'p' function vectors gets preatty grotesque giving me wierd output...
- i tried plotting them both using ezplot function but for some reason the sampled vector i got form xt_otot_q3 shows only as a straight line at 0.
any ideas how i should plot them together? to plot the xt_otot_q3 function i must create a vector if i try to plot it directly using ezplot it gives me the following eror:
>> ezplot(xt_otot_q3, [-10 10])
Error using xt_otot_q3 (line 2)
Not enough input arguments.
thanks in advance.
If I am understanding it properly, you have two functions p, and xt_otot_q3. You want to plot them together.
syms t;
func1 = xt_otot_q3(t);
ezplot(func1, [-10 10]);
# retain current graph, for new graph
hold on;
# symbolic function p
ezplot(p, [-10 10]);
I hope it helps.