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 次查看(过去 30 天)
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 个评论
roya afshar
roya afshar 2018-12-15
编辑:madhan ravi 2018-12-15
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?

请先登录,再进行评论。

采纳的回答

madhan ravi
madhan ravi 2018-12-17
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

更多回答(1 个)

roya afshar
roya afshar 2018-12-17
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?

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by