필터 지우기
필터 지우기

Second Order Ordinary Differential Equation Array

조회 수: 1 (최근 30일)
Matthew Tom
Matthew Tom 2019년 4월 5일
댓글: Star Strider 2019년 4월 5일
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))

채택된 답변

Star Strider
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
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
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개)

Matthew Tom
Matthew Tom 2019년 4월 5일
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!

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by