How to convert a For loop function into an If function

I have a question. As i am using a MATLAB function block. I cannot use recursive functions like for loop and while loop. Therefore, I need to replace my code using If function. I have a code which has an for loop within a while loop function as shown below. How to i convert it to If function or is there other ways to not use recursive function?
while abs((max(Pnew)-min(Pnew))/max(Pnew))>0.05
for i = 1:4
f = 0.3 + (0.5-0.3)*B;
vnew(i) = vold(i)+(DPold(i) - DPbest)*f;
if DPold(i) > DPbest(i)
DPnew(i) = DPold(i) - abs(vnew(i));
else
DPnew(i) = DPold(i) + abs(vnew(i));
end
if rand(0,1)>rold(i)
DPnew(i) = DPold(i) + mean(Aold)*e;
end
D = DPnew(i);
Pnew(i) = P;
if Pnew(i) < Pold(i)
DPnew(i) = DPold(i);
Pnew(i) = Pold(i);
end
Anew(i) = 0.5*Aold(i);
rnew(i) = rold(i)*(1-exp(-0.5));
end
end
DPbest = max(DPnew);
D = DPbest;

댓글 수: 1

Guillaume
Guillaume 2019년 7월 11일
편집: Guillaume 2019년 7월 11일
for, while, if are not functions. They are statements. A function in matlab will always start with the statement function, or starts with an @(.
for and while have nothing to do with recursive functions. Usually you would replace a recursion by a for or while loop. The code you show does not use recursion.
The purpose of for and while is repeating a task. The purpose of if is choosing alternative tasks. Completely different purpose. You can't swap one for the other.
It's really unclear what your problem is and what you want to achieve.

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

 채택된 답변

Jon
Jon 2019년 7월 11일
First, I think when you are saying "recursive" you mean "looping". While and For loops aren't inherently recursive. A recursive function calls itself. In any case, assuming you want to rewrite your code without loops you are "vectorizing" your MATLAB code. One of the great powers of MATLAB compared to other programming languages is its ability to operate on entire vectors or matrices without needing loops. If you want to get the benefits of using MATLAB it is important to be able to take advantage of this power. I would suggest you start by reading https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
Just a few ways you could use vectorization in your code include replacing
vnew(i) = vold(i)+(DPold(i) - DPbest)*f
with
vnew = vold + (DPold-DPbest)*f
You can also use "logical indexing" which selects just those elements of a vector, or matrix that meet a certain logical criteria, to replace for example
if Pnew(i) < Pold(i)
DPnew(i) = DPold(i);
with
idl = Pnew < Pold
DPnew(idl) = DPold(idl)
Finally, just a suggestion for future posts, it is good to use the Code button in the MATLAB Answers toolbar to include code. That way it comes out nicely formatted, and is easy to copy, so that respondents can try out your code.

댓글 수: 5

I've fixed the formatting of the question.
I think you're right and the OP is asking how to vectorise the code. Using the proper terms is important.
Thanks Jon and Guilaume for you answers.
I actually have an array of 4 different duty cycles which i need to run through the code one at a time. And after running through all 4 then I use the while statement to check if the 4 new duty cycles in that same array fits the condition i set. So is my code considered recursive or not? If it is then how do it alter it to not make it recursive? I am implementing this function block into a solar PV system with a DC-DC converter. This code is part of a MPPT algorithm which will generate the best duty cycle for the system. Sorry not much information was provided. Thanks for the vectorization suggestion. Its good.
No, there is no recursion in your code, just loops. Recursion is when a function is calling itself, e.g.
function f = factorial(n)
if n > 1
f = n * factorial(n-1); %recursion: factorial calls itself
else
f = 1;
end
end
A recursive function can always be converted to a while loop, so there is no longer any recursion:
function f = factorial(n) %not recursive anymore
f = 1;
while n > 1
f = f * n;
n = n-1;
end
end
However, I'm not sure why you care if your function is recursive or not.
Part of your code can be vectorised which will make it faster, as pointed out by Jon. A while loop cannot generally be vectorised, and there is nothing wrong with having while loops.
Hi Guillaume,
Just want to make sure as my lecturer mentioned that while and for statements are recursive calls after he read that 'Recursive calls are not allowed in MATLAB Function blocks.' in the help section of Matlab function block. What you and Jon explained to me is new so I need to make sure that my code is actually fine. Thank you so much!
No, while and for is definitively not recursion, your lecturer is wrong on that point. while and for are loop constructs. Again, recursion is a function that call itself. From the first line of the wikipedia article on recursion:
Recursion (adjective: recursive) occurs when a thing is defined in terms of itself or of its type.
It would make sense that recursion is not allowed in Simulink function blocks. It wouldn't make sense that something as basic and necessary as a for loop wouldn't be.

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

추가 답변 (0개)

카테고리

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

질문:

2019년 7월 11일

댓글:

2019년 7월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by