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)

 采纳的回答

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

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by