how would i create a loop within a loop

for example, if I had 3 functions say, alpha, beta and pi
we have the boundary conditions that pi(0) = 0 and pi(1) = 1.
now we have pi(i+1) = alpha(i)*pi(i) + beta(i)*pi(i-1)
where alpha(i) = 5*pi(i) + 2
and beta(i) = pi(i)*pi(i-1) + 1/2
my question is how do a create a loop within a loop which automatically solves this for all i

 采纳的回答

First, let's just define our functions. For alpha( i ) and beta( i ), we would get code that might look something like:
function y = alpha(i)
y = 5 * pi(i) + 2;
end
function z = beta(i)
z = pi(i) * pi(i-1) + 1/2;
end
Now we have to write our equation for pi( i ). To make our lives a little simpler, instead of writing it as:
pi( i + 1 ) = alpha( i ) * pi( i ) + beta( i ) * pi( i - 1 )
let's subtract 1 from each input to rewrite it as:
pi( i ) = alpha( i - 1 ) * pi( i - 1 ) + beta( i - 1 ) * pi( i - 2 )
Now that This would then give us the code:
function x = pi(i)
%Boundary Case: pi(0) = 0
if(i == 0)
x = 0;
%Boundary Case: pi(1) = 1
elseif (i == 1)
x = 1;
%if i > 1, use our equation
else
x = alpha(i - 1)*pi(i - 1) + beta(i - 1)*pi(i - 2);
end
end
Now we can do something like
>>> pi(2)
ans =
7
Now all you need to do is call the function pi in a loop.
If you are interested, another way that you could do this, would be to calculate values of pi and save the value in an array each time.
That way when you try to calculate pi( i ), you can just loop up the values of pi( i - 1 ), p( i - 2 ), etc in that array.

4 个评论

hi, thank you very much for this, it has helped so much, im slightly confused though as i fully copied your coding however when i wrote
pi(2)
i got an error saying: Error using pi Too many input arguments.
a similar problem occurs with beta(i), however, alpha(i) seems to work fine
I actually ran into that exact problem when I was first trying it out!
A solution to that would be to put the all the code into a file. Copy and paste it into the editor making sure that the code from the second box I gave you comes before the code in the first box in your file. Then save that as "pi.m"
Then to actually use the code, make sure you are in the same folder that your file is in.
To do this, you can right-click the little tab above the editor that says "pi.m" and select "Charge Current Folder to ..."
Then try running the command pi(2).
Hopefully that helps!
Even better is not to call the function pi, since it's already a function that comes with matlab.
Using pi as a function (or variable) name is very foolish. Soon enough you'll write y = cos(pi/2) in some unrelated code and wonder why it raises an error or give incorrect result. (Answer: because it's calling your pi function you've forgotten about instead of the built-in pi).
awesome! this has worked perfectly, what i done was create 3 scripts in the same folder hence allowing me to find all values for alpha,beta and pi
@guillaume what i done was label my pi function as Pi in order to avoid future confusion.
I have one last question though, how can i create a loop for say the first 10 terms and be able to plot the alpha and beta terms on a matrix, say beta on the diagonal and alpha on the diagonal above and below

请先登录,再进行评论。

更多回答(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