Second Order Ordinary Differential Equation Array
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2019-4-5
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 个评论
Star Strider
2019-4-5
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!
Star Strider
2019-4-5
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!
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!