user1477107
user1477107

Reputation: 47

How to create if-then logic in simulink?

I have a written code and a program in matlab, but I want to do the same think in simulink. I am stuck at the if-then part of the program which I can't figure out.

I tried using the "If" block, but then I didn't know how to say "then alfa=0". After trying to research it I saw many different ways to do conditional statements and now I'm very confused.

if(vb-y(2))*y(3)<0
    alfa=0
end

if y(3)<zba && (vb-y(2))*y(3)>=0
    alfa=0
end
if zba<y(3) && y(3)<zmax && (vb-y(2))*y(3)>=0
    alfa=0.5*(sin(pi*(y(3)-(zmax+zba)/2)/(zmax-zba))+1)
end
if zmax<y(3) && (vb-y(2))*y(3)>=0
    alfa=1
end

I basically need this made in simulink. If someone could just start it out for me or explain the proper way to do this I'd be very thankful.

Upvotes: 3

Views: 8692

Answers (1)

avermaet
avermaet

Reputation: 1593

I believe this can be done in the following way: Choose the if-block to implement your condition, and for the then and else-block, you choose an if-action-block. The if block decides which of these two if-action blocks will be evaluated at every timestep and the following merge-block integrates both individual outputs into one combined signal (alfa in your case) again. The image shows the first if-then of your code (assuming else alfa=1. In case of no else, the else-output can be disabled in the block-properties).

image showing solution Since the if-block supports elseif expressions as a comma separated list, I suggest you use this for your 2nd, 3rd and 4th if's, which means that you will most likely need additional if-action blocks, and more inputs for the merge-block. Also for the elseif expressions, you'll need to change your && to & which should be fine. a && b only evaluates b if a is true, a & b always evaluates both.

Upvotes: 5

Related Questions