How is this an exponential curve?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
I would like your help on the following code.
How does this trace an exponential curve? N=8000; % number of steps to take T=8; % maximum time h=T/N; % time step t=(0:h:T); % t is the vector [0 1h 2h 3h ... Nh] y=zeros(size(t)); % prepare place to store locations
y(1)=3; % initial height
for i=1:N % start taking steps
y(i+1)=y(i)-y(i)*h;
end;
plot(t,y), hold on % plot more permanently
y(1)=-2; % initial height
for i=1:N % start taking steps
y(i+1)=y(i)-y(i)*h;
end;
plot(t,y); % plot more permanently
Please help.
Thanks
0 件のコメント
採用された回答
Shoaibur Rahman
2015 年 1 月 14 日
編集済み: Shoaibur Rahman
2015 年 1 月 14 日
Convert your difference equation into differential equation:
y(i+1) = y(i)-y(i)*h;
y(i+1) - y(i) = -y(i)*h;
(y(i+1) - y(i))/h = -y(i);
dy/dt = -y
Time step h is replaced by dt to make the equation more understandable. Now, solve this differential equation using variable separation method:
dy/y = -dt
ln|y| = -t + C
y = exp(-t+C)
y = A*exp(-t)
At t = 0, A = y(0) = 3, in your case, y(0) is equivalent to y(1) in Matlab
y = 3*exp(-t)
This means that y is exponential. However, when solving using difference equation, make sure that h or the time is small enough to keep the solution stable. For this particular equation, h<1 . Use a larger value of h in your code just to see the effect.
0 件のコメント
その他の回答 (1 件)
Torsten
2015 年 1 月 14 日
The above code is an implementation of the explicit Euler method to solve the differential equations
y'=-y, y(0)=3
y'=-y, y(0)=-2
The above differential equations have solutions
y(t)=3*exp(-t)
y(t)=-2*exp(-t)
So yes, the program generates approximations to exponentially decaying curves.
Best wishes
Torsten.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Oceanography and Hydrology についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!