I am having some problems with my function code. The main idea is to get parameter SI (main unknown) in which the equation Q_cal-Q=0. Can somebody help me?
Thanks a lot.
P=1.94;
Q=1.09;
P5=1.08;
fc=0;
lambda=0.2;
Ts=24;
[SI]=singhandyu(P,Q,P5,lambda,Ts,fc);
function [SI] =singhandyu(P,Q,P5,lambda,Ts,fc)
Fc=fc.*Ts;
f=#(SI)((P5-0.2*SI)*SI)./(P5+0.8*SI);
M=#(SI)max(f(SI),0);
S=#(SI)(SI-M(SI));
Ia=#(SI)lambda.*S(SI);
Q_cal=#(SI)((P-Ia(SI)-Fc).*(P-Ia(SI)-Fc+M(SI)))./(P-Ia(SI)-Fc+M(SI)+S(SI));
H=#(SI)Q_cal(SI)-Q;
S0=0;
SI_sol=fsolve(H,S0)
end
Almost all your anonymous functions need SI as an input, but you are not passing the argument when you call a previously defined function.
To clarify, f requires one input argument,
f=#(SI)((P5-0.2*SI)*SI)./(P5+0.8*SI);
but when calling f in the next line, you are not providing it:
M=#(SI)max(f,0);
So make sure you pass the argument to each function call:
M=#(SI)max(f(SI),0);
S=#(SI)max(SI-M(SI),0);
etc.
Related
I'm trying to check the input of a Matlab function to see whether the user has forgotten about it or not (which is easy to do in this case).
If the user has not supplied number_obs then I want to pause the program and wait for the user to input this information.
Some other StackOverflow posts seem to suggest using ~exist however this doesn't seem to work. Can anybody suggest what I'm doing wrong here?
function output=test(number_obs)
if ~exist('number_obs'),
number_obs=input('How many observations do you have in your experiments?')
end
The Python equivalent would be something like:
def test(number_obs):
if nummber_obs != None:
output=raw_input('How many observations do you have in your experiments? :')
return output
You can do this with nargin
function output=test(number_obs)
if nargin<1
number_obs=input('How many observations do you have in your experiments?')
end
(Edited to correct the truth) It may not matter here, but to be on the safe side, you should always specify the type of object you're checking. In your case, its a 'var', so
if ~exist('number_obs','var'),
Thanks to dasdingonesin for pointing this out.
Suppose I want to declare some variables then declare a function:
x = 2;
function y = function(x)
y = (x^2)+1;
end
y = function(x);
disp(y)
Matlab returns the error "Function keyword use is invalid here..."
Why can't I declare variables or write any text before declaring a function? Is there good reason or is it a quirk?
EDIT:
To clarify, I do know how to get around this problem (but thanks for the suggestions nonetheless) but I suppose I'm asking why the Matlab team made this decision. By making a function declaration the first line of a file, does it have implications for memory management, or something?
The REPL prompt of Scala can have a function defined after a variable. So this is a choice (a quirk if you want) from Matlab's inner internals.
If a function is defined in a file, there are two possibilities:
The main function of that file. Then the file must begin with the function declaration: in your example, function y = fun(x). I'm using fun as the function's name. I don't think function can be used as a function's name.
See here for more details.
A nested function. In this case, the function declaration and definition can be within another function of the preceding case.
See here for more details.
As you can see, in either case the file begins with a function declaration (namely that of the main function).
The function can also be defined as an anonymous function. Then no declaration is needed, and the function can be defined anywhere. But there's a restriction: the function can contain only a single statement (so it cannot define internal variables other than the output). Therefore this method can only be used for simple functions.
In your example, the function could be defined anonymously as fun = #(x) x^2+1.
See here for more details.
Others have given good info about nested functions and such.
But the reason for the error you get is that "function" is a reserved word in Matlab. You cannot have a function with this name.
function y = my_function(x)
y = (x^2)+1;
end
And stick it in another file called my_function.m
I'm wondering if there is a way to know by what function is called my function "fun_a" when running fun_a. I know that i could track the "parent function" by sending information as an argument to the "child function" but i would like to avoid that if possible.
Thanks a lot
One way is using dbstack:
% In a sub-function or function called by another
st = dbstack;
st(1).name % The function's name
st(2).name % The function caller's name (parent)
...
Another useful function is mfilename if you happen to just want the name of the main function and M-file in which a sub-function resides.
No idea what the computational cost of these are, but I imagine that simply passing in the function name is going to be cheaper even if less elegant.
I have a function that returns two values, like so:
[a b] = myfunc(x)
Is there a way to get the second return value without using a temporary variable, and without altering the function?
What I'm looking for is something like this:
abs(secondreturnvalue(myfunc(x)))
not that i know of. subsref doesn't seem to work in this case, possibly because the second variable isn't even returned from the function.
since matlab 2009b it is possible to use the notation
[~, b] = function(x)
if you don't need the first argument, but this still uses a temporary variable for b.
Unless there is some pressing need to do this, I would probably advise against it. The clarity of your code will suffer. Storing the outputs in temporary variables and then passing these variables to another function will make your code cleaner, and the different ways you could do this are outlined here: How to elegantly ignore some return values of a MATLAB function?.
However, if you really want or need to do this, the only feasible way I can think of would be to create your own function secondreturnvalue. Here's a more general example called nth_output:
function value = nth_output(N,fcn,varargin)
[value{1:N}] = fcn(varargin{:});
value = value{N};
end
And you would call it by passing as inputs 1) the output argument number you want, 2) a function handle to myfunc, and 3) whatever input arguments you need to pass to myfunc:
abs(nth_output(2,#myfunc,x))
I see a function passing like
sigma = 3*e-2
svmTrain(...,#(X,y)gaussianKernel(X,y,sigma),...);
What is going on with such a function passing, would someone explain ?
The syntax #(X,y) gaussianKernel(X, y, sigma) creates an anonymous function by binding the third argument of this existing function guassianKernel(X, y, s) to specifically be the value sigma.
If you inspect the svmTrain function signature, you'll see that it allows passing in a function, which is where this anonymous function is going.
Two things happen here:
First is function passing. For example, you had a function foo in your code, and you want to pass it as a parameter. In this case, you use # operator.
function MainScript
goo(#foo);
end
function goo(fHandle)
fHandle();
end
function foo
disp('Hello world!');
end
The second is anonymous functions. Anonymous function is a function much like every other function, except that it is defined at runtime, it has no name, and it binds to itself a local copy of variables that are passed to it. (For more information, see Closure). For example:
function MainScript
foo = #() (disp('Hello world!'));
goo(#foo);
end