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!!

 Réponse acceptée

Joseph Cheng
Joseph Cheng le 22 Août 2014
Modifié(e) : Joseph Cheng le 22 Août 2014
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 commentaires

Olimpia
Olimpia le 22 Août 2014
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);
Olimpia
Olimpia le 22 Août 2014
NEVER MIND!!!!! it worked! thank you very much

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by