how to update value of a function at each time step

18 次查看(过去 30 天)
Hi everyone, I am really stuck in creating a code that updates the value of my function K(t) at each time step and plots it. Seems quite simple but cant get my head around it.
at t=0 K(old)=3
at t=1 K(new_1)=K(old)+dt*dK/dt
at t=2 K(new_2)=K(new_1)+ dt*dK/dt
where dK/dt=4*exp(-30*(t - 3).^2)
for each subsequent time step we update the previous value with dK/dt
I would like to plot K(new) for t=0:0.01:10
Thanks!!

采纳的回答

Joseph Cheng
Joseph Cheng 2014-8-22
编辑:Joseph Cheng 2014-8-22
So we start off with something like this
dt = 0.01;
t=0:dt:10;
K = zeros(size(t)); %initialize it
K(1) = 3;%setup initial value
for ind=2:length(t)
K(ind) = K(ind-1)+dt*dK/dt; where t in your dKdt is subbed by t(ind);
end
figure,plot(t,K);
So what is performed above is that in the for loop i start at index of 2 to however long your t variable is. such that the t needed for your t=2 is the t(2) built by the t=0:dt:10; then as you can see from the equation i am looking at the current index and that is equal to the previous value + the defined 0.01 value for dt and the dK/dt.
  2 个评论
Olimpia
Olimpia 2014-8-22
Thanks!However when i try it in matlab i get
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Untitled2 (line 8) K(ind) = K(ind-1)+dt*D; where t in your dKdt is subbed by t(ind);
maybe it is something I am doing wrong.... I set dk/dt=4*exp(-30*(t - 3).^2);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by