v(t) is not defined help me!

m=70
g=10
c=10
t=0;
v(0)=0;
while t<7*log(100)
t=t+0.1
v(t+1)= v(t)+0.1*(g-((c/m)*v(t)));
end
but the error is happened
how can i change v(0)=0;

1 commentaire

Rik
Rik le 18 Mar 2019
You are confusing indexing with a function call.

Réponses (1)

Rik
Rik le 18 Mar 2019
Modifié(e) : Rik le 18 Mar 2019

0 votes

This code should work:
m=70;
g=10;
c=10;
t=0;
v=0;n=0;
while t<7*log(100)
t=t+0.1;
n=n+1;
v(n+1)= v(n)+0.1*(g-((c/m)*v(n)));
end
%OR:
m=70;
g=10;
c=10;
t=0:0.1:7*log(100);
v=zeros(size(t));
for n=2:numel(t)
v(n)=v(n-1)+0.1*(g-((c/m)*v(n-1)));
end
plot(t,v)

9 commentaires

I would create a vector for ‘t’ as well:
t(n+1)=t(n)+0.1;
Although not used in the calculation, it will likely be used in a subsequent plot:
figure
plot(t, v)
grid
Rik
Rik le 18 Mar 2019
It feels like this could be vectorized, but I can't see how. The sum is fine, you can use cumsum on the separate terms, but the factor is a bit too tricky for me now.
Star Strider
Star Strider le 18 Mar 2019
You have likely vectorized it as much as possible. I doubt that completely vectorizing a recursive function is an option.
Rik
Rik le 18 Mar 2019
Judging by the shape, I suspect this is one of those recursive functions that resolve to some exponential equation. But it is probably more trouble than it's worth for a limited number of calls/resolution.
t is irrelevant here.
v = @(n) -m*g/c * (((10*m-c)/(10*m))^n - 1);
@Rik —
syms v(t) t c m g
Eqn = diff(v) == v + 0.1*(g-((c/m)*v))
V = dsolve(Eqn, v(0) == 0)
V =
(g*m - g*m*exp(-(t*(c - 10*m))/(10*m)))/(c - 10*m)
Walter Roberson
Walter Roberson le 18 Mar 2019
Star Strider: that gives very different values. If you evaluate for say t = 5/10 (that is, n=5) then it gives a result of about 0.65 whereas the iterative version gives a result about 4.86 .
The iterative result exactly matches the formula I posted.
The formula I posted can be changed to time through the substitution replacing n with t*10
Star Strider
Star Strider le 18 Mar 2019
@Walter —
I was responding to Rik’s earlier Comment (link).
Walter Roberson
Walter Roberson le 18 Mar 2019
Yes, but the exp() form you posted is not anywhere close to the actual solution.

Cette question est clôturée.

Tags

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by