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

Rik
Rik 2019년 3월 18일
You are confusing indexing with a function call.

답변 (1개)

Rik
Rik 2019년 3월 18일
편집: Rik 2019년 3월 18일

0 개 추천

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

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 2019년 3월 18일
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 2019년 3월 18일
You have likely vectorized it as much as possible. I doubt that completely vectorizing a recursive function is an option.
Rik
Rik 2019년 3월 18일
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 2019년 3월 18일
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 2019년 3월 18일
@Walter —
I was responding to Rik’s earlier Comment (link).
Walter Roberson
Walter Roberson 2019년 3월 18일
Yes, but the exp() form you posted is not anywhere close to the actual solution.

이 질문은 마감되었습니다.

태그

질문:

2019년 3월 18일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by