This question already has answers here:
What is the # operator (at sign) in MATLAB?
(3 answers)
#(t) mean in Matlab? [duplicate]
(2 answers)
Closed 4 years ago.
I was just browsing through a code and I found the following line :
other_function(#(t)(xx(t,g)))
where other_function,xx are already defined functions and g is already defined.
Here is the code for xx
function [val]=xx(x,y)
val=x+y;
end;
SO now I am unable to understand the meaning of #(t)(xx(t,g))
It is a function handle. It is useful to pass functions as parameters. You can find more in the MATLAB documentation
Just an example: suppose you have a simple function
function y = computeSquare(x)
y = x.^2;
end
than you can compute an integral in this way:
q = integral(#computeSquare,0,1);
In your example: other_function declares as an input parameter a function t and another parameter called g.
Related
This question already has answers here:
MATLAB not enough input arguments
(2 answers)
Closed 5 years ago.
In Matlab, I want to get the variables from the workspace for the function . But I did not do it.
For example; the function is:
function Y = objfun(x)
Y = 20+x(1).^2 + 2*x(2).^2 -15*x(3);
end
gives me the following problem when I run the function
>> objfun
Not enough input arguments.
Error in objfun (line 5)
Y = 20+x(1).^2 + 2*x(2).^2 -15*x(3);
x variable is exist in workspace like x= [4 5 7] and I don't want to write it inside of function. so what shall I do.
Maybe it is very east question for you but I don't know and I trid make it.
could you help me?
In Matlab (or Octave) you can use scripts or functions.
If you create script called objfun, you have what you are looking for. Just call it using objfun and it will use workspace variable x. The script is saved as objfun.m.
Functions are different. They can have arguments, but these arguments are local variables (only available within the function).
If you define a function, you must call it with the arguments.
This question already has answers here:
How to force MATLAB to return all values in a nested function call?
(4 answers)
Closed 6 years ago.
I'm looking to pass the outputs of a two-output function into a two-input function, in one line.
i.e. if I have two functions
function [out1, out2] = funA(in)
%function definition here
function out = funB(in1, in2)
%function definition here
I want to do something like
out = funB(funA(in)) %this doesn't actually work
Is there syntax to do this without having to write it as
[o1, o2] = funA(in)
out = funB(o1, o2)
I'm also not looking for
[o1, o2] = funA(in); out = funB(o1, o2);
I'm not sure this is possible as if you call the function in-line with another call, Matlab will always assume that you only want the first/primary output.
Matlab only creates the other output variables (out2/in2 here) if you actually assign them.
This question already has an answer here:
MATLAB: How do I pass a parameter to a function?
(1 answer)
Closed 6 years ago.
I have a function (for an SIR model), and then a script that solves this function and compares it to the data I am trying to fit this model to. Thus I am trying to run a for loop to change a parameter in the function in order to optimize the fit. I'm wondering how to change my the (r) and (a) parameters in a for loop without having to change them by hand:
function ydot=epidemic(t,y)
r=0.000001;
a=1/3;
ydot=zeros(3,1);
ydot(1)=-r*y(1)*y(2);
ydot(2)=r*y(1)*y(2)-a*y(2);
ydot(3)=a*y(2);
end
and
[t,y]=ode45('epidemic',[0:222], [70500,1,0])
Thanks
You can use the following: you add the r and a parameter to your function
function ydot=epidemic(t,y,r,a)
ydot=zeros(3,1);
ydot(1)=-r*y(1)*y(2);
ydot(2)=r*y(1)*y(2)-a*y(2);
ydot(3)=a*y(2);
end
and then pass the function to ode45 like that
r = 0.000001 ;
a = 1/3 ;
[t,y]=ode45(#(t,y)epidemic(t,y,r,a),[0:222], [70500,1,0])
Basically, #(t,y)epidemic(t,y,r,a) defined a new function with arguments (t,y) and where r and a are using the values defined just above.
Then you can put all of that in a for loop.
This question already has answers here:
suppressing output variables in matlab
(2 answers)
Closed 8 years ago.
If I have a function a that accepts 2 parameters (double) in Matlab as follows
function [x,y] = a(z)
What does the symbol "~" do when the function is called with this handle as follows
[x,~,y] = a[10]
Thanks
The "~" symbol in matlab is logical NOT. So it's basically like ignoring that output/input. For example, if I have a line of code like this:
[out1,~,out3] = function(vargin);
the second output is not kept or stored anywhere for later use. For more info, type "help ~" in the command window.
This question already has answers here:
How to elegantly ignore some return values of a MATLAB function
(8 answers)
How do I get the second return value from a function without using temporary variables?
(2 answers)
Closed 9 years ago.
I'm not very good at programming and new to matlab, so sorry if I'm not using the right terminology.
If I use the e.g. fminbnd procedure in Matlab first i get the x-value which minimises and then i get the function value. Is there a neat way for me to get just the minimum function value.
To make it clear, for me it seems I have to do:
[x,y] = fminbnd(h,-10,10)
when I only need y. Is there any way for me to not get x?
Use ~ to suppress x output. Only available in later versions of matlab (=> r2009b).
[~, y] = fminbnd(h, -10, 10);