Solve ODES with multiple initial conditions

4 views (last 30 days)
global tau;
global T0; To=120+298
global V_R; V_R=2*1e-5
function dy=ODES(z,y)
%ODES
dy(1)=V_R*(-2*r(1)-2*r(2));
dy(2)=V_R*(-0.5*r(1));
dy(3)=V_R*(r(1)-r(3));
dy(4)=-V_R*(DELH1*R(1))/y(1)/CPA
dy=dy';
dy=dy(:);
end
CT0=100000/8.3145/T0;
v0_0=V_R/tau;
FT0=v0_0*CT0;
R0=[8 1 0]/(8+1);
C0=R0*CT0;
F0=v0_0*C0;
tspan=[0,1];
y0=[F0,T0];
[z,y]=ode15s(@ODES,tspan,y0);
I want to sovle the ODES with differen tau from 0.1 to 20 and than plot the tau versus y(2) at the z=1. Does someone know how to do it.
Appreciate your help
  3 Comments
Zhihong Lin
Zhihong Lin on 30 Jul 2019
Hello Walter,
I am sorry that these code makes you confused. i just randomly delete lots of codes because the original code is kind of long to post. could you help me how to vary the initial conditions so that i could plot like what i said before. thank you very much.
Walter Roberson
Walter Roberson on 30 Jul 2019
Edited: Walter Roberson on 30 Jul 2019
Where do you define r and R that you use in the function? And DELH1 and CPA? Where do you initialize T0? You initialize To which is a different variable.

Sign in to comment.

Answers (1)

Athul Prakash
Athul Prakash on 9 Aug 2019
Hey Zhihong Lin,
As has been pointed out by others, your initialization code is a bit confusing and buggy. I understand that you posted only a shortened version here. Hence, I am assuming that, given any value of tau, you have the code to generate all the required parameters for the Diff Equations.
Three things I think you should consider:
1) For-Loop:
Have you tried a simple for loop to iterate through your range of tau; each time calling ode15s to solve the problem for a different initial condition. Here’s what I mean, through pseudo-code:
tau_vector = 0.1:step:20; %choose your step value
for tau_i = tau_vector:
% Here, Initialize all your variables that depend on tau_i.
[z, y] = ode15s(@ODES, tspan, y0);
% Here, add y(2) into a vector which would hold the values of all iterations.
end
% now you can plot the record of y(2) against tau_vector.
Ofcourse, if you have too many values in tau_vector, this approach might take a long time. In that case, consider the following methods to speed it up.
2. Use parfor
parfor, from the Parallel Computing Toolbox, lets you run multiple iterations of a for loop in parallel (on different CPU cores). Refer to the documentation below:
The iterations of the loop are independent of each other, hence parfor is suitable here, if required.
3. Vectorize all your variables into 1 ODE System
If you have N values of tau, treat your differential equations as a 4xN variable problem.
Instead of 4 variables, y(0) to y(4), use y(0) to y(4*N). Vectorize all the variables required and use ode45 to solve all equations simultaneously, as if they were a single system of 4*N equations.
Depending on your exact code, this might take an even longer time though. But I think it’s worth trying out.
Good Luck!

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!