Second Order Ordinary Differential Equation Array

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))

 Respuesta aceptada

Star Strider
Star Strider el 5 de Abr. de 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 comentarios

Matthew Tom’s ‘Answer’ moved here:
Hmm... It didn't get exactly as I wanted, but now I think that I have an idea of what to do. Thank you for response and I appreciate the help!
Matthew Tom’s ‘Answer’ moved here:
And to answer your question about the objective of the code, I was able to include the first point D(1,1) into the graph, but the question is how to include the other two points D(1,2) and D(1,3) to form two other graphs.
The goal was to graph three functions on the same figure by changing the value of D, which was the three points that I listed.
The goal was to graph three functions on the same figure by changing the value of D, which was the three points that I listed.
My version of your code does exactly that.
Hmm... It didn't get exactly as I wanted, but now I think that I have an idea of what to do. Thank you for response and I appreciate the help!
My pleasure.
What did you want? Not everything is possible.
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.
Thank you for your help!
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!

Iniciar sesión para comentar.

Más respuestas (1)

Matthew Tom
Matthew Tom el 5 de Abr. de 2019

0 votos

I forgot to mention that the main error is with the array. I am uncertain how to approach the plotting and the function script when the variable, D, is an array. Thank you for taking your time to look through my code!

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Abr. de 2019

Comentada:

el 5 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by