f******e 发帖数: 921 | 1 I want to solve a system of ODE, for example,
x1'=x2-x1^2+x3^2
x2'=-x1*x3-2*x1*x2+x3
for x3, it satisfy, x1^2 + x2^2+x3^2=10;
my code is below:
function xp = F(t,x)
xp = zeros(3,1);
xp(1)= x(2) -x(1)^2+ x(3)^3;
xp(2) = - x(1)*x(3) - 2*x(1)*x(2) + x(3);
x(3) = sqrt(10-x(1)^2-x(2)^2);
[t,x]=ode45(@F,[0,10],[0;1;1]);
plot(t,x)
my figure shows x3 is a straight line, what is the mistake?
How to correct the mistake in my code? thank you very much!!! | f******e 发帖数: 921 | 2 Who can give me some advice?thanks a lot
【在 f******e 的大作中提到】 : I want to solve a system of ODE, for example, : x1'=x2-x1^2+x3^2 : x2'=-x1*x3-2*x1*x2+x3 : for x3, it satisfy, x1^2 + x2^2+x3^2=10; : my code is below: : function xp = F(t,x) : xp = zeros(3,1); : xp(1)= x(2) -x(1)^2+ x(3)^3; : xp(2) = - x(1)*x(3) - 2*x(1)*x(2) + x(3); : x(3) = sqrt(10-x(1)^2-x(2)^2);
| m***n 发帖数: 581 | 3 你实际上只有x(1),x(2)两个状态变量。xp(3)始终是0,所以不会更新。最后算的x(3)
是不会传递到x中去的。
我把你的程序改成两个状态变量,x3作为输出变量了:
主程序:
[t,x]=ode45(@F,[0,10],[0;1]);
x3 = sqrt(10-x(:,1).^2-x(:,2).^2);
plot(t,[x x3])
子程序:
function xp = F(t,x)
xp = zeros(2,1);
tmp = sqrt(10-x(1)^2-x(2)^2);
xp(1)= x(2) -x(1)^2+ tmp^3;
xp(2) = - x(1)*tmp - 2*x(1)*x(2) + tmp;
【在 f******e 的大作中提到】 : I want to solve a system of ODE, for example, : x1'=x2-x1^2+x3^2 : x2'=-x1*x3-2*x1*x2+x3 : for x3, it satisfy, x1^2 + x2^2+x3^2=10; : my code is below: : function xp = F(t,x) : xp = zeros(3,1); : xp(1)= x(2) -x(1)^2+ x(3)^3; : xp(2) = - x(1)*x(3) - 2*x(1)*x(2) + x(3); : x(3) = sqrt(10-x(1)^2-x(2)^2);
| f******e 发帖数: 921 | 4 thank you very very much!!!
【在 m***n 的大作中提到】 : 你实际上只有x(1),x(2)两个状态变量。xp(3)始终是0,所以不会更新。最后算的x(3) : 是不会传递到x中去的。 : 我把你的程序改成两个状态变量,x3作为输出变量了: : 主程序: : [t,x]=ode45(@F,[0,10],[0;1]); : x3 = sqrt(10-x(:,1).^2-x(:,2).^2); : plot(t,[x x3]) : 子程序: : function xp = F(t,x) : xp = zeros(2,1);
|
|