While Loop if Condition is True

Hello Everyone,
I'm just confused about how to set a 'while loop' that works only when my condition is true. I need to create a 'quarter sine signal' while a condition (a derivative input) is true.
Actually, I'm trying to create a signal on Simulink, but it looks like it can't be handled w/o using editor functions.
Basically, I need to build a signal which gives 7500 if condition is false. If condition is true, there should be a sine function initiated and lasts until condition gets false.
In Simulink, I link my condition via 'If' block and send it to 'Signal Builder' block, however in order to create sine wave, I put Sine Wave block and feed the builder with it by using another if block. Problem is; Sine Wave function starts with simulation time and when my condition is true, it doesn't start from zero, it only gives the sine input at that simulation time.
In Matlab editor, it tried below w/o success;
function (y)
while y>0
a=0:0.01:pi/4
y=sin(a)
else
y=7500
end
end
Sorry for this long query, but can someone help me on solution for this?
Thank you in advance.

댓글 수: 2

Image Analyst
Image Analyst 2019년 1월 3일
편집: Image Analyst 2019년 1월 3일
But your y is a vector (many, many values) because "a" is a vector. y is not a single true or false value like "while" expects. Under what condition do you want to break out of the while loop?
Burak Akyol
Burak Akyol 2019년 1월 8일
편집: Burak Akyol 2019년 1월 8일
Hi there, thanks for your reply and sorry for my late response.
I have an initial signal which gives only 1 or 0.
This signal should be given as input to my function.
->In my function, while input signal is 1;
there should be a sine wave function run from 0 to pi/2 with 0.01 step
and sine function results should be my function output
->when signal turns out to 0
my function output should be 7500.
Would that make my issue a bit clear?
Thanks

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

답변 (1개)

Cris LaPierre
Cris LaPierre 2019년 1월 3일
편집: Cris LaPierre 2019년 1월 3일

0 개 추천

Check out this answer. It might help you out.
Also, you need to give your function a name and a variable to output:
function out=myFunc(y)
What are typical inputs for y?

댓글 수: 3

Burak Akyol
Burak Akyol 2019년 1월 8일
Hi Cris, thanks for your reply and sorry for my late response.
I think, your link is not directly linked to my issue. I posted a detailed explanation of my issue to above comment. I'm still trying to configure w/o success.
Thanks.
Cris LaPierre
Cris LaPierre 2019년 1월 8일
No, it was mostly there to show you an example of a while loop.
If you are really new to programming in MATLAB, I'd strongly recommend you invest a couple hours to go through MATLAB Onramp. Even if you don't do it all, look at the chapters and go through those that seem applicable to what you need right now.
I don't think you need a while loop. You need an if statement
y =1;
y = myFunc(y)
function out = myFunc(y)
if y>0
a=0:0.01:pi/4;
out=sin(a);
else
out=7500;
end
end
Burak Akyol
Burak Akyol 2019년 1월 8일
편집: Burak Akyol 2019년 1월 8일
Dear Cris,
I appreciate your answer and your advise on MATLAB Onramp. I will definitely go over the videos.
Actually, the reason that I was trying to prefer 'while' was; Simulink. I have a model in Simulink which I'm trying to implement this algorithm into. Initially I had used 'If' block, it was like; if condition is applied, run the sine wave, if doesn't; link to a constant which is 7500 (it seemed a way to me). However when I saw the scope, I realized that sine wave had started and ended with simulation time and gave sine wave output which corresponded to those intervals.
It was like I ran a sine wave during my simulation time and focused on results at specific time intervals.
I only initiate a sine wave at a time interval that my condition is applied. Do you have a solution in Simulink blocks?
That's why I assigned a 'fcn' block to Simulink and started to do something in editor.
Sorry for bothering you for multiple times and thank you for your all help.

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

카테고리

제품

릴리스

R2018a

질문:

2019년 1월 3일

편집:

2019년 1월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by