Second Order Ordinary Differential Equation Array
1 view (last 30 days)
Show older comments
Hi. A thought never occurred to me about how to approach graphing multiple ordinary differential equations simultaneously while changing a parameter (in an array).
Here is my function code:
function h = manometer(t,y)
rho = 1.2; %Density(g/cm^3)
g = 981; %Gravitational acceleration (cm/s^2)
L = 250; %Liquid length in the manometer (cm)
mu = 0.0097; %Viscosity of the fluid (g/cm/s)
D = [0.1 0.2 0.35]; %Diameter of the pipe (cm) for 1st, 2nd, and 3rd cases
Y = 10; %Deviation variable in the change of the depth in cm (dP/(rho*g))
h(1)=y(2);
h(2)=(3*g*Y/(2*L))-(24*mu*y(2)/(rho.*D.^2))-(3*g*y(1)/(2*L));
h = h';
Hre is my Script File:
t = [0 20];
initial = [0, 0];
[t,y] = ode45(@manometer, t, initial);
plot(t,y(:,1))
0 Comments
Accepted Answer
Star Strider
on 5 Apr 2019
I’m not certain what you’re asking.
If you want different results for different values of ‘D’, the easiest way is to add ‘D’ as an additional parameter, then call ode45 in a loop. This works best with a vector of times for ’tspan’ since all the row sizes of the output will be the same.
Example:
function h = manometer(t,y,D)
rho = 1.2; %Density(g/cm^3)
g = 981; %Gravitational acceleration (cm/s^2)
L = 250; %Liquid length in the manometer (cm)
mu = 0.0097; %Viscosity of the fluid (g/cm/s)
% % D = [0.1 0.2 0.35]; %Diameter of the pipe (cm) for 1st, 2nd, and 3rd cases
Y = 10; %Deviation variable in the change of the depth in cm (dP/(rho*g))
h(1)=y(2);
h(2)=(3*g*Y/(2*L))-(24*mu*y(2)/(rho.*D.^2))-(3*g*y(1)/(2*L));
h = h';
end
D = [0.1 0.2 0.35]; %Diameter of the pipe (cm) for 1st, 2nd, and 3rd cases
t = linspace(0, 20, 25);
initial = [0, 0];
for k = 1:numel(D)
[t,y{k}] = ode45(@(t,y)manometer(t,y,D(k)), t, initial);
end
ym = cell2mat(y);
plot(t,ym(:,1:2:end))
lgd = sprintfc('D = %.1f', D);
legend(lgd, 'Location','SE')
Experiment to get the result you want.
5 Comments
Star Strider
on 5 Apr 2019
Matthew Tom’s ‘Answer’ moved here:
Yes, your function did what I wanted to by graphing out the three graphs, but the graphs aren't as smooth as plotting it out by hand. But I figured out a solution to the code and it works perfectly.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/212246/image.png)
Thank you for your help!
Star Strider
on 5 Apr 2019
To get higher resolution, increase the number of points in the linspace call:
t = linspace(0, 20, 250);
If my Answer helped you solve your problem, please Accept it!
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!