How is this an exponential curve?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
cgo
am 14 Jan. 2015
Bearbeitet: Shoaibur Rahman
am 14 Jan. 2015
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 Kommentare
Akzeptierte Antwort
Shoaibur Rahman
am 14 Jan. 2015
Bearbeitet: Shoaibur Rahman
am 14 Jan. 2015
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 Kommentare
Weitere Antworten (1)
Torsten
am 14 Jan. 2015
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 Kommentare
Siehe auch
Kategorien
Mehr zu Oceanography and Hydrology finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!