How to calculate differential equation with trigonometric function?

9 views (last 30 days)
Matas Rimkus
Matas Rimkus on 19 May 2022
Answered: KSSV on 19 May 2022
How can I solve this differential equation using matlab? The answer can be in analytical, symbolic or numerical solution form. This is what I have done:
syms y(x)
ff=1+((2/y)*x+(x^2)*(y^2)*cos(y))*diff(y,x)==0;
y(x)=dsolve(ff)
And it shows y(x) = [empty sym], which is not a proper answer that I was requires to get. Also, I've tried using other solution methods:
f=@(x,y) ((2/y)*x+(x^2)*(y^2)*cos(y))*diff(y,x)==0;
[x,y]=ode45(@(x,y),f,[0 2],1);
plot(x,y)
But this shows invalid expression error and I don't know where the problem is.

Accepted Answer

KSSV
KSSV on 19 May 2022
f = @(x,y) -1./((2./y).*x+x.^2*y.^2.*cos(y)) ;
xspan = [0.1 2] ;
x0 = 1 ;
[x,y] = ode45(f, xspan, x0);
plot(x,y)

More Answers (1)

Sam Chak
Sam Chak on 19 May 2022
With the given ODE
function dydx = odefcn(x, y)
fcn = 2*x/y + (x^2)*(y^2)*cos(y);
dydx = - 1/fcn;
end
you can run the ode45 solver and plot the output. Note that or else singularity occurs.
h = 1e-4;
xspan = 0.1:h:10; % simulation x-range
init = 1;
[x, y] = ode45(@(x, y) odefcn(x, y), xspan, init);
plot(x, y, 'linewidth', 1.5)
grid on
xlabel({'$x$'}, 'Interpreter', 'latex')
ylabel({'$y$'}, 'Interpreter', 'latex')
title('System response')

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!