MATLAB: Size of output ode45

differential equationsif statementloopodeode45

please help me with this problem,
i want to solve ODEs many times with different value of "v2" variable,
so i use if statement to loop it, but the output of X2 look wrong especially its size (41×4)
i try 1 time solving with one value of "v2" only, it turns out size of X2 is about 6000×4
here's the code
x_extend = zeros(121,1);
x_compress = zeros(121,1);
v2=0;
for i=0:1:120
v2=v2+i;
save input.mat
t_end = 2.5;
TSPAN = [0 t_end]; %Solving duration
IC = [0; 0; 0; 0]; %Initial conditions
[T2, X2]= ode45(@PTVP2, TSPAN, IC,options);
a=X2(:,3)-X2(:,1);
x_extend(i+1) = max(a);
x_compress(i+1) = min(a);
end

Best Answer

You are not saving values of X in each iteration
x_extend = zeros(121,1);
x_compress = zeros(121,1);
v2=0;
Ts = cell(121,1);
Xs = cell(121,1);
for i=0:1:120
v2=v2+i; % is this correct??? Or do you want v2=i???
save input.mat
t_end = 2.5;
TSPAN = [0 t_end]; %Solving duration
IC = [0; 0; 0; 0]; %Initial conditions
[T2, X2]= ode45(@PTVP2, TSPAN, IC,options);
a=X2(:,3)-X2(:,1);
x_extend(i+1) = max(a);
x_compress(i+1) = min(a);
Ts{i} = T2;
Xs{i} = X2;
end
This creates cell array T2 and X2 which save results for each iteration.