I want to restrict the variable that I use as input for a matlab function block, it must be only able to increase.
To achieve that i have tried to compare the variable and the previous sample of it in a matlab function, but i don't know how to create two inputs. To solve that i've tried to use a mux, but then i get an error. And google doesn't give me an explanation how to use a mux signal as input for a matlab function.
So that leaves me here with this low-level question.
Thanks in advance for your help and time. Cheers.
To use multiple variables in a function, you need to modify your function declaration at the first line of your function. The reference syntax is:
function [y1,...,yN] = myfun(x1,...,xM)
where x1 through xM are inputs. Your declaration with two inputs might look something like:
function [returnValue] = hasIncreased(previousSample, variable)
See the Matlab Function Documentation for more information.
Related
I am going to try and explain myself simply in the hopes of getting a simple answer.
Let's say I have a function 'calculate' that takes the inputs [t,k,r,x] and outputs [A,B,C,D] as follows:
function [A,B,C,D] = calculate(t,k,r,x)
Now lets say I have another function that takes these outputs as the inputs, and spits out more, different outputs, eg.
function [M,N] = again(A,B,C,D)
How do I link [M,N] to say k and t? The overall aim is to minimise both M and N by optimising k and t, and I can guess that it has something to do with nested functions and passing parameters but I'm not sure how to start, and the start is all I want. Thanks
Take a look at the Matlab optimization toolbox. It provides functions for a multitude of optimization problems. Although I believe these functions only take one function as a parameter. Therefore for your case it would probably be best, if you do it like this if you can:
Write function calculate.m with parameters (t,k,r,x) and save it.
Write function again.m with parameters(t,k,r,x) and save it.
Function again calls function calculate with parameters (t,k,r,x) and then continues to determine (M,N) from the output of calculate.
in the Matlab toolbox optimization function, e.g.: fmincon(fun,x0,A,b), you then have to use again.m as the function you want to optimize (fun).
Hope that's good enough for a start.
I would like to know how to find all MATLAB function blocks in a Simulink model.
In a model in which I know there are a lot of MATLAB function blocks, I tried the following command:
find_system(myModel,'LookUnderMasks','on','FollowLinks','on','BlockType','MATLAB Function')
However, this command returns an empty cell array. If I try something like this:
find_system(myModel,'LookUnderMasks','on','FollowLinks','on','BlockType','Gain')
, I'm getting many results. Is the "MATLAB Function" an actual BlockType or is there another term to use?
You can use the following code to find all MATLAB function blocks.
S = sfroot();
% Get block handles
B = find(sfroot, '-isa','Stateflow.EMChart');
This will search all open models and returns a list of objects of type Stateflow.EMChart. You can look at the Path property of these objects to reduce the list to the model you want.
You will need to define the search depth use the below:
h=find_system(myModel,'SearchDepth',N, 'regexp', 'on', 'FollowLinks','on','Findall','on','LookUnderMasks','all', 'BlockType','Gain');
Specify N, the higher the N the deeper into the blocks the search will be.
Let me know if this doesn't work.
How do I easiest solve an equation=0 with a function as a parameter?
My function with one input variable is called potd(angle), with one output variable, potNRGderiv. I tried:
syms x
solve(potd(x))
This gave me error: Undefined function 'sind' for input arguments of type 'sym'.
Have you got any ideas? Thanks in advance.
solve is the wrong avenue here, unless your function can be rewritten as a simple equation. solve uses muPAD functions which is why you can do solve(sin(x)) but not solve(sind(x)). You can, of course, just do the conversion yourself.
If your function is more complicated or you'd rather not rewrite it, look into fsolve:
x = fsolve(#myfun,x0)
Where x0 is your initial guess - i.e. myfun(x0) is close to 0 - and myfun is a function which takes x and returns a single output. Depending on what your function does, you may have to adjust the options using optimoptions (tolerance, step size, etc) to get a good result.
I am building a reduced order observer in MATLAB. The matrices are calculated using functions/script files outside matlab and simulink function blocks are using these functions to calculate values.
The problem is that some commands like 'acker', 'place' etc which used to work on command window/function/script files are not working in simulink function block and showing errors.
I tried using simin block to take these matrices from workspace but it is also showing errors which I can't understand.
Thanks for your help.
If I get your question correctly then, from User defined functions, you could add a Matlab function block with the following code:
function fcn(in)
%#codegen
coder.extrinsic('acker', 'place')
# Now you can use acker, place so add more code.
When using a multiple-output matlab function, do i need to callback all variables? or can I just take the first two variables? (if so..is it not recommended?)
lets say in function.m
[a, b, c] = function( )
in main.m
[var1, var2] = function;
When calling (almost) any function in matlab you can request fewer outputs than it specifies. So, yes the example you give should work perfectly fine.
There are some clever things you can do with this, such as using nargout within a function to see how many output arguments have been requested and only calculating the values that have been requested as an optimisation trick.
It depends on the definition of the function, and exactly which of the outputs you want to get.
Not all the function allow to do it, you can find all the options for each function in the beginning of the help documentation on the specific function.
If you want only the 2nd, or 3rd outputs, and you want also to save the computation-time of the results that does not interesting, you can use ~ option, like this (for versions 2009b and later):
[~, var1, var2]=function
Many functions allow for options to passed that change how the function behaves. I used/wrote various numerical solving functions a bit and one that nice amount of option, for instance is the LSMR function(s).
Otherwise, if you can manipulate the original either introduce an input(s) to do so before or at the end with an inline subroutine to generate the outputs you want.
Or if you can't it will return as either a cell array or a vector and you can pass an anonymous function to generate the desired outputs that way.
Really, can be done many ways. Very contextual.