Clear Filters
Clear Filters

I have a set of second order differential nonlinear equation and I want to solve them together with ode45, how could I write codes for them?or better to say I want to define state transition function for such equations.

2 views (last 30 days)
first three equations all variable and constants are not in vector or matrix form but 4th equation is a 3by1 vector like this:
x''=y'+y+x-(x)/(x^2+y^2+z^2)^1.5+1
y''=x'-x+y-y/(x^2+y^2+z^2)^1.5
z''=-z/(x^2+y^2+z^2)^1.5
m''=[x'' y'' z'']'+[1 2 3]
and I want to write function for this system like this function that i have found in one of matlab example:
function dxdt = vdpStateFcnContinuous(x)
%vdpStateFcnContinuous Evaluate the van der Pol ODEs for mu = 1
dxdt = [x(2); (1-x(1)^2)*x(2)-x(1)];
end
  2 Comments
roya afshar
roya afshar on 15 Dec 2018
Edited: madhan ravi on 15 Dec 2018
I dont have Latex but I write like this:
syms x(t) y(t) z(t) m(t)
eqn1 = diff(x,t,2) == diff(y,t)+y+x-((x)/(x^2+y^2+z^2)^1.5)+1;
eqn2 = diff(y,t,2) == diff(x,t)-x+y-((y)/(x^2+y^2+z^2)^1.5);
eqn3 = diff(z,t,2) ==-(z)/(x^2+y^2+z^2)^1.5;
eqn4 = diff(m,t,2) == [eqn1 eqn2 eqn3]'+[1;2;3];
latex(eqn1)
latex(eqn2)
latex(eqn3)
latex(eqn4)
is it ok?

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 17 Dec 2018
I believe :
[t,x]=ode45(@eqns,[0 10],[1;0;0;0;0;0;0;0;0;0;0;0]); % function call
plot(t,x(:,1)) % to plot the solution
function dxdt = eqns(t,x) % function definition
dxdt = [x(2);
x(4)+x(5)+x(1)-(x(1)/((x(1)^2+x(3)^2+x(5)^2)^1.5))+1;
x(4);
x(2)-x(1)+x(3)-(x(3)/((x(1)^2+x(3)^2+x(5)^2)^1.5));
x(6);
-x(5)/((x(1)^2+x(3)^2+x(5)^2)^1.5);
x(2:2:6);
[x(4)+x(5)+x(1)-(x(1)/((x(1)^2+x(3)^2+x(5)^2)^1.5))+1;x(2)-x(1)+x(3)-(x(3)/((x(1)^2+x(3)^2+x(5)^2)^1.5));-x(5)/((x(1)^2+x(3)^2+x(5)^2)^1.5)]+[1;2;3]];
end

More Answers (1)

roya afshar
roya afshar on 17 Dec 2018
function dxdt = eqns(x)
one = 7:9;
two = 10:12;
dxdt = [x(2);
x(4)+x(5)+x(1)-(x(1)/((x(1)^2+x(3)^2+x(5)^2)^1.5))+1;
x(4);
x(2)-x(1)+x(3)-(x(3)/((x(1)^2+x(3)^2+x(5)^2)^1.5));
x(6);
-x(5)/((x(1)^2+x(3)^2+x(5)^2)^1.5);
x(tw0);
[x(4)+x(5)+x(1)-(x(1)/((x(1)^2+x(3)^2+x(5)^2)^1.5))+1;x(2)-x(1)+x(3)-(x(3)/((x(1)^2+x(3)^2+x(5)^2)^1.5));-x(5)/((x(1)^2+x(3)^2+x(5)^2)^1.5)]+[1;2;3]];
end
Could it be true answer? Could anyone make me sure about the answer please?

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!