Info
This question is closed. Reopen it to edit or answer.
How to solve simultaneous ODE equations in matlab with if/else if?
1 view (last 30 days)
Show older comments
Hello, I have a set of ODE and I am writing the matlab code to solve it but unfortunately I don't get the right result, i.e., it becomes very slow. Could you please tell me where I am making mistake? I even used ode23s, but still the problem exists.
Here is the ODE:
surface.
And here is my matlab code:
function [ x,y,z ] = lorenz_like( alpha, beta, gamma,theta,const, initV, T, eps )
%This function generates data from Lorenz-like system
if nargin<5
error('MATLAB:lorenz:NotEnoughInputs','Not enough input arguments.');
end
if nargin<6
eps = 0.000001;
delta_t=0.01;
T =0:delta_t:100;
initV = [40 40 0];
end
options = odeset('RelTol',eps,'AbsTol',[eps eps eps/10]);
[T,X] = ode45(@(T,X) F(T, X, alpha, beta, gamma,theta,const), T, initV, options);
x = X(:,1);
y = X(:,2);
z = X(:,3);
return
end
function dx = F(T, X, alpha, beta, gamma,theta,const)
dx = zeros(3,1);
x_1p=-14;
x_2p=-12;
x_3p=0;
x_1o=0;
x_2o=0;
x_3o=5;
x_1n=14;
x_2n=12;
x_3n=0;
A=[-alpha alpha 0;
beta-x_3p -1 -x_1p;
x_2p x_1p -gamma];
A_x=[X(1)-x_1p;
X(2)-x_2p
X(3)-x_3p];
B=[-alpha alpha 0;
beta-x_3o -1 -x_1o;
x_2o x_1o -gamma ];
B_x=[X(1)-x_1o;
X(2)-x_2o
X(3)-x_3o];
C=[-alpha alpha 0;
beta-x_3n -1 -x_1n;
x_2n x_1n -gamma];
C_x=[X(1)-x_1n;
X(2)-x_2n
X(3)-x_3n];
condition= X(1)*tan(theta)+X(2);
if condition>const
dx=A*A_x;
elseif -const<=condition && condition<=const
dx=B*B_x;
else
dx=C*C_x;
end
return
end
Answers (0)
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!