Running the code takes too long to finish(Euler's Method)

I'm trying to create a code to use Euler's method in solving the differential equation as shown below. When I try to run the code, it takes a long time to do each iteration. I feel I have made a mistake when declaring variables. I have also placed a picture of a segment of the supposed output of the code which I got from my professor. Any insight would be appreciated.
rho=1000;
g=9.8;
A_tank=3.13;
A_pipe=0.06;
C=pi/12;
K1=300;
K2=1000;
syms dh(t,h)
dh(t,h)=(K1+K2*sin(5*C*t)*cos(C*t)-rho*A_pipe*(2*g*h)^(1/2))/(rho*A_tank);
t=0;
tn=150;
h=3;
s=0.1;
ts=t:s:tn;
hwt=[];
fprintf('time \t\t height \n')
fprintf('%f \t %f\t \n',t,h)
for i=1:numel(ts)
h=h+dh(t,h)*s;
t=t+s;
fprintf('%f \t %f\t \n',t,h);
hwt(i)=h;
end
plot(ts,hwt)

 採用された回答

Alan Stevens
Alan Stevens 2021 年 1 月 26 日

1 投票

Remove
syms dh(t,h)
and replace
dh(t,h)=(K1+K2*sin(5*C*t)*cos(C*t)-rho*A_pipe*(2*g*h)^(1/2))/(rho*A_tank);
with
dh = @(t,h)(K1+K2*sin(5*C*t)*cos(C*t)-rho*A_pipe*(2*g*h)^(1/2))/(rho*A_tank);
to get

1 件のコメント

Marco Serrano
Marco Serrano 2021 年 1 月 26 日
Thank you! That worked well for me.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by