matlab function variable definition [duplicate] - matlab

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.

Related

why do i get fminsearch undefined function error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to optimize a function with 2 inputs. Trying to use fminsearch but it keeps saying undefined function or variable although it is already defined.
I have already defined the function in a separate script that is in the same directory with my main script. I have a classroom license which includes optimization toolbox and there is no spelling mistake while calling the function.
function o=u(x,y)
%some code here
end
%in a second script
init=[0.1,0.1];
b=fminsearch(o,init);
The error is:
Undefined function or variable 'o'.
From the documentation on fminsearch, the function being minimized must have a single argument and accessed with a function handle (see this related answer).
The error you are getting is because you can't call o and use it as an input to fminsearch() since o is undefined. To get o, you have to first get u(x,y), plus as mentioned, fminsearch requires a function handle as an input.
You have several options that still use your standalone function, u(x,y).
1. Create a function handle
Define a function handle that calls u(x,y) but has a single argument which is a 2 x 1 vector, z = [x; y].
fh =#(z) u(z(1),z(2));
z0 = [1; 2]; % Initial Guess for z = [x; y]
[z,TC] = fminsearch(fh,z0)
2. Change the function and call it directly
The same result is possible with
[z,TC] = fminsearch(#u,z0)
if you redefine u(x,y) to be as follows:
function o=u(z)
x = z(1);
y = z(2);
% your function code here
end

# in Function definition in Matlab [duplicate]

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.

Passing two values from one Matlab function to another in one line [duplicate]

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.

How to change parameters in a function when using ode45 in MATLAB [duplicate]

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.

Function inside script in Matlab? [duplicate]

This question already has answers here:
In MATLAB, can I have a script and a function definition in the same file?
(7 answers)
Closed 9 years ago.
Can I have fast and short helper local function to use inside script?
Currently I have "FUNCTION keyword use is invalid here" message.
Why?
This is correct, MATLAB does not allow you to define full functions in a script. However, there are at least two solutions that may help you:
You could turn the script into a function. Workspace variables you are referring to from within your scripts would become arguments of the function, and you could return certain result variables.
If your helper function is really small, you may be able to define it in your script as an anonymous functions without the function keyword, as in poly = #(x) x.^2 + 3 * x - 4; - a polynomial function, for example.