If else condition is not working properly in matlab function block

function [y1,y2]= fcn(u1,u2,u3,u4)
if u1 > 5
y1=u2;
else y1=u3;
end
if u1 < 5
y2=u3;
else y2=u2;
end
I implemented this function in MATLAB function but my output of MATLAB function block always comes as 5 it is not taking the input properely and it is also showing 5 when it is really less than 5 also,here iam giving this output to controlled current source.

12 Comments

Why have you used 2 separate if-else blocks for checking the condition on the same boundary point i.e. 5?
What are the inputs you have provided to call the function i.e. the values of u1, u2, u3 and u4?
What is the use of u4 in the function?
"it is not taking the input properely"
What do you mean by this?
function [y1]= fcn(u1,u2,u3)
if u1 > 5
y1=u2;
else y1=u3;
end
When i give like this also same error persists.
Iam trying to control the controlled current source based on the ouput of MATLAB function,but whenever i kept the thershold as 5 or 10 it is showing same as the output.
You didn't show how you are calling the function.
>> fcn( 2, 3, 4 )
ans =
4
>> fcn( 6, 3, 4 )
ans =
3
both seem to work as expected for me with the second, smaller, function you showed. You should generally put the code on a separate line to the else though, for readability. It doesn't affect the result, but it is more confusing to read as you have it.
I've never used Simulink so I don't know how that works, but the function logic internally doesn't appear to be the problem, it would seem more like you need to track down what inputs are going in and whether these are what you expect them to be. I assume it is possible to debug code in Simulink?
function [y1]= fcn(u1,u2,u3)
if u1 > 5
y1=u2;
else
y1=u3;
end
end
Be better/more consistent with formatting and will be easier to read/debug code as @Adam suggested earlier. Also, use the "CODE" formatting button to format your code here...
The above function has nothing wrong with it, per se, whatever issues you have are somehow tied to the model -- I note the output of the function block is highlighted in yellow and says "Inaccessible" -- that implies to me something, somehow isn't right, but I don't have and have never used Simulink so can't comment/help on the actual process of using a MATLAB function in/with it, but there would seem to be where your problems lie...clearly if the function is being called with the desired inputs, it will as far as it alone is concerned return either one or the other of the two inputs based on the first.
If it isn't, then either it isn't getting called or the inputs aren't what you think they are or the fact there seems to be a warning/error message on the model about that output signal means it is just using some substitute value instead (effectively the first case above of not being called).
Methinks you need to create a very simple model that calls a function and get it to work standalone to understand that use first...
What is the expected behaviour of the code when u1 is exactly equal to 5 ?
From the screenshot, I think that the conditional statement in your code can be mathematically interpreted as follows:
.
u2 is the output from the built-in PI controller block, but u3 is the output from the block mask of the custom PI controller. Can you show what is under the block mask?
That's a good Q? as far as user expectations, Walter; I had just ensured it did have a path both ways and didn't leave out the specific value case. If @Nandhini expected it to change from <5 to exactly 5, then his first thinking it didn't work would be explained, but the original would return
y1=u3;
y2=u2;
if u1=5 identically whereas if is either > or < 5 the two y both have same selected u.
I have attached the plots of the u1 and y1 signals (Scope 2 and Scope 3, respectively). The code in the MATLAB Function block works as expected. What is the desired signal for y1? It may be possible to construct the function mathematically.
Blocks under the Masked system (Speed regulator PI controller):
u1 signal:
y1 signal:
Hi thank you for the comment.I have another doubt,i want to check this loop after the half of simulation time,so that i can avoid intial oscillations.for example i want to avoid the first 0.5s simulation time for this MATLAB function loop,kindly help me how this can be implemented.

Sign in to comment.

Answers (1)

If you want me to provide better guidance, please tell me more about your goal for writing this function.
function [y1,y2]= fcn(u1,u2,u3,u4)
if u1>=5
y1 = u2 ;
else
y2 = u3 ;
end
end

4 Comments

That's not the function he simplified to and says still not functional...and
function [y1,y2]= fcn(u1,u2,u3,u4)
if u1>=5
y1 = u2 ;
else
y2 = u3 ;
end
end
is NOT what the original actually reduces to; while inefficiently written with the two independent blocks it is the same as
function [y1,y2]= fcn(u1,u2,u3)
if u1>5
y1 = u2;
y2 = u2;
else
y1 = u3;
y2 = u3;
end
end
also removing the superfluous input argument.
ADDENDUM: As @Walter Roberson questions, as written the original will do the "else" clause for u1==5; that may not have been the intended action in which case if the case of 5 were tested, there wouldn't be any change from the u1<5 case, but it should have behaved differently than u1==10.
As for intent, that seems pretty clear from his earlier statement -- I would have to try to dig into the actual model much more to see if the logic in its construction is such as to accomplish that, but the intent seems clear enough:
"I am trying to control the controlled current source based on the ouput of MATLAB function,but whenever i kept the thershold as 5 or 10 it is showing same as the output." --@Nandhini
The problem isn't in the function per se (as revised) but in its use in the model -- and there I've no hands-on experience at all, but the "inaccessible" message on the output of the function block signal trace would seem to be a very big klew something ain't right...
Hi thank you all for your comments.My basic intention is to control the controlled current source based on the output of MATLAB function which in turn depends on two PI controller,i.e if u>5 one PI control should work,if u<=5 one PI control should work,but my currrent problem is which iam giving as a thershold for example u>5 or u<5 the output of controlled current source is 5 if i give u>10 or u<10 the output of controlled current source is 10.I dont know what mistake iam doing.Please help in this regard.
@Nandhini, Can you show share the Simulink file? Use the paperclip icon to attach the file.

Sign in to comment.

Categories

Find more on Simulink in Help Center and File Exchange

Products

Release

R2023a

Asked:

on 28 Sep 2023

Commented:

on 29 Sep 2023

Community Treasure Hunt

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

Start Hunting!