Finding zero without interp1.

a=[8000];
for x=2:25
a(x)=[(0.97*a(end))-((250*(x-1)/x))];
end
z=1:25;
plot(z,a)
table(z', a')
I have two questions
  1. How to find the value of z when a is zero? (Without using interp1) Can I use fzero? Or a For loop & If-Else?
  2. How do I end the table when Var2=0? (The last row in the table should end with 24.8579 and 0. I do not want to include negatives.)

댓글 수: 3

Matt J
Matt J 2022년 6월 5일
편집: Matt J 2022년 6월 5일
How to find the value of z when a is zero? (Without using interp1)
The sequence a(k) is defined only for integer k. Unless a(z)=0 for z also an integer, you will have no choice but to define some kind of interpolation between integer points.
Walter Roberson
Walter Roberson 2022년 6월 5일
or to decouple the storage location from the x value... like I show in my Answer
Matt J
Matt J 2022년 6월 5일
I would argue that that is a choice of interpoaltion...

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 6월 4일

0 개 추천

You will need a loop, unless you can solve the recurrence relationship. (MATLAB does not offer any tools for recurrence relationships.)

댓글 수: 4

Capami
Capami 2022년 6월 4일
Could you please provide the code for that?
x = 2;
while a(end) > 0
a(end+1)=[(0.97*a(end))-((250*(x-1)/x))];
x = x + 1;
end
This will resolve down to an integer.
x = 2;
for dx = 10.^-(0:5)
while a(end) > 0
a(end+1)=[(0.97*a(end))-((250*(x-1)/x))];
x = x + dx;
end
x = x - dx;
a(end) = [];
end
At the end of this, a(end) will be the last positive a before the zero crossing, and x will be the last x before the crossing. The accuracy will be 1e-5 in this code.
Walter Roberson
Walter Roberson 2022년 6월 5일
One modification you might want to make here is to keep an x history so that later you can plot(xhist, a) since the a array entries are no longer equidistant in x space.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2022년 6월 4일

댓글:

2022년 6월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by