use a Loop to repeat an equation using the previous answer as the new variable.

I want to write a loop to do the below all the way up to p340....I do not want to change the name of the variable each time but instead would like all variables in one array. I have been searching and trying to figure this out for hours. I've written so many different forms of a for loop I don't know what to include here.
xt = 100:440
p1 = 99977
p2 = p1./(exp((50)./(29.3.*((xt)))))
p3 = p2./(exp((50)./(29.3.*((xt)))))
p4 = p3./(exp((50)./(29.3.*((xt)))))
....etc

댓글 수: 4

I would like to close my other question but don't know how. I'd like for this question to stay open.
So anyhow you have wasted one of the answerers effort.
I would have closed the question sooner if i knew how...maybe the site should make it easier to close a question and efforst would not have been wasted.

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

 채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 5월 4일
편집: KALYAN ACHARJYA 2019년 5월 4일
xt=100:440;
p={};
p{1}=99977;
for i=2:340;
p_iter=p{i-1};
deno=exp(50./(29.3*(xt)));
p{i}=p_iter./deno;
end

댓글 수: 3

Ok...this give me the correct values...however it gives it to me in a 1X340 cell where each cell contains a 1X340 array...the first value of the array in that cell is the correct value for that iteration...but I just need one array not a bunch of cells filled with a bunch of arrays.
Ok so one small change and it's solved...thanks!
xt=100:440;
p={};
p{1}=99977;
for i=2:340;
p_iter=p{i-1};
deno=exp(50./(29.3*(xt(i))));
p{i}=p_iter./deno;
end
".the first value of the array in that cell is the correct value for that iteration.."
That is quite interesting, because the first values of each vector corresponds to the first value of the xt vector. So effectively you want to ignore all of the other xt values. Is this correct?
EDIT: you have now accepted KALYAN ACHARJYA's complex answer, which according to your own comment does not do what you want. You wrote: ".the first value of the array in that cell is the correct value for that iteration" and now you have shown in your own comment (by the addition of xt(i) indexing) that what you described is not what you want at all.
Also note that KALYAN ACHARJYA does not provide a numeric vector, as you requested.
Simpler answer:

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by