which simulink blocks can I use to create the function below ??

Please which simulink blocks can I use to create the function below ?
fsolve(@(XY)anothertrial(xy,A,B,P1,P2),XY0)
Where A,B, P1 and P2 are variables that will be provided by the results of calculations in other blocks, and XY0 is the value of the guess for the function fsolve.

 Accepted Answer

Function handles were not supported in Embedded MATLAB Function blocks until fairly recently, additionally, fsolve is not amongst the functions supported for code-generation, which are the only functions that can be used directly in a Embedded MATLAB Function block.
The easiest way to do this might be to use an (Interpreted) MATLAB Function block and enter this in the dialog:
fsolve(@(xy)anothertrial(xy,u(1),u(2),u(3),u(4)),xy0)
You can either create the guess xy0 as a workspace variable, or if it is a constant, enter the value directly. Additionally, concatenate your inputs A,B,P1,P2 into a single input using a Mux or Concatenate block, because the Interpreted MATLAB Function block accepts only one input.

11 Comments

Yes, It is working !!!!!
Thank you Kaustubha Govind for your very clear answer. I did exactly as you told me and it is working.
I am really new in Simulink and Matlab but thanks to friends like you I am making good progress in my project.
Thanks a million !!!!!!!.
Hi,
I think I have a rather similar problem.
I would like to perform the following 2 lines of code in the MATLAB Function found in the Simulink library (user-defined function):
F = @(x) eval(costFunction);
[x,fval] = fmincon(F,x0,[],[],Aeq,beq,lb,ub);
where costFunction is a string generated from another rather long and complicated function.
However, I also received a coder error: 'This kind of expression is not supported'. I have read your solution, but am still unable to understand how to apply it in my case.
Any help or suggestion is greatly appreciated! Thanks alot!!
Hi
I am trying to integrate fsolve function into simulink by using code.extrinsic. The equations contain a variable, which is calculated by other blocks,need to be transfer to the equation. Function handles were not supported in Embedded MATLAB Function blocks. How can I transfer the variable to the equations to be solved?
function x=main(w)
x0=[-5;-5];
[x,fval]=fsolve(@objfun1,x0,[ ],w)
function f=objfun1(x,w)
f=[2*log(x(1))-x(2)-sin(x(1))+w;
exp(x(1))+x(2)-3*cos(x(2))+w];
where w is the variable need to be transfered from other block. can you help me? best regards
Use the Interpreted MATLAB Function block to call the function main - 'w' will be the input signal to the block.
Thanks a lot! it works now
Interpreted MATLAB Function block works well when I test it alone. But when I integrate it into a larger system, error occurs: "Objective function is returning undefined values at initial point. FSOLVE cannot continue". How can I debug the problem? could you please answer my question?
Not sure. You could try setting a breakpoint in main to see what's going on.
Thank you very much! I tried your methed, and i found that the one of the inputs of fsolve function is NaN, i solve the problem by introducing a memory block. i do not know whether the following point is correct or not: the simulation starts at the evaluation of fslove function,so the initial values set in the fsolve function cannot be delivered to next block for calcutaion.
Besides,the outputs of fsolve function are complex and the iteration is rather slow, but my expectation is real number, how can i deal with it?
Huan: Your question is getting rather long for a comments discussion. Can you start a new question please? Thanks!
How to concatenate inputs if they are all vectors ? Can we still access those vectors as u(1), u(2), u(3) ?

Sign in to comment.

More Answers (2)

You can use either the block Matlab fcn in the "user-defined function" library either a sfunction.
I suggest you to use the first one, in this way you just need to write your function inside a m file and put it as argument of the block
for further details take a look to the doc http://www.mathworks.nl/help/toolbox/simulink/ug/f6-106261.html

7 Comments

Hi grapevine,
Thank you for your answer.
I have already tried using the block "MATLAB Fcn".
In the matlab function space in the block parameters I wrote
fsolve(@(XY)anothertrial(xy,A,B,P1,P2),XY0)
but still remaining is the problem of the fsolve guess XY0 and the variables A, B, P1, and P2. How can they be connected to the "MATLAB Fcn" block in a way that guarantees that they are read by the fsolve function ??
you can define them as global in the workspace or if you prefere you can pass these variables as input (mux) 2 the Matlab fcn block, in this way your "u" will be a vector
good luck
But the mux block does not allow to give names to the signals, how the "MATLAB Fcn" block will make a difference between the values of XY0, A, B, P1 and P2 ???
as I said in this way your "u" will be a vector in the Matlab fcn and you can refer to your parameters as
u(1)=A
u(2)=B
......
In the embedded MATLAB Editor of the block "Embedded MATLAB Function" I wrote the following code:
function XY = lost(A,B,P1,P2,xy0)
XY = fsolve(@(xy)anothertrial(xy,A,B,P1,P2),xy0)
and after adding constants blocks and giving them the values of A,B,P1,P2 and xyo and connecting them to the block of "Embedded MATLAB Function" I ran the simulation and I received the following error message:
This kind of expression is not supported.
Function 'Embedded MATLAB Function' (#31.47.78), line 2, column 13:
"@(xy)anothertrial(xy,A,B,P1,P2)"
Is there any solution for this ?????
u should not use the embedded matlab function 'cause it limited u should use the MATLAB Function block as I told u
u have to change the name of the associated Matlab function, for default it's sin u have to write the name of your mfile where you wrote your function
good luck
Yes, It is working !!!!!
Thank you grapevine for your support during these last days.
I am really new in Simulink and Matlab but thanks to friends like you I am doing good progress in my project.
Thanks a million !!!!!.

Sign in to comment.

i am trying to generate PWM by watching the video from youtube, i am unable to use fcn block as they have used in the video , i am getting an error stating Warning: Using a default value of 0.2 for maximum step size. The simulation step size will be equal to or less than this value. You can disable this diagnostic by setting 'Automatic solver parameter selection' diagnostic to 'none' in the Diagnostics page of the configuration parameters dialog .
please some1 help me solving this .
Best regards,

1 Comment

This warning has nothing to do with the Fcn block, it is simply complaining that you have not set a "maximum step size" in the Configuration Parameters window's Solver pane. If you'd like to ignore the warning, you can disable it by following the directions in the warning message.
(Btw, this is a warning, not an error, so you should still be able to simulate and obtain your result)

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!