helo, I have the following function called stat.m
function [mean,stdev] = stat(x)
n = length(x)
mean = sum(x)/n
stdev = sqrt(sum((x-mean).^2/n))
I defined x as a vector which is [1,2,5,7,9]
how come when I type a = stat(x), matlab returns a = 5 for the last line at command prompt?
If you want to get both return values, you have to do this:
[a, b] = stat(x);
If you just do a = stat(x), MATLAB interprets that to mean that you only want the first return value.
because a gets the first argument mean
try to call it [a,b] = stat(x)
Related
I wrote a function in octave and got an error like this:
parse error near line 6 of file D:\Evan\Kuliah\Smt 4\METNUM\newton_method.m
syntax error
y = #3*x^2 - 4*x;e
code:
function y = df(x)
y = #3*x^2 - 4*x;
end
I've changed the function to something like this
function y = df
y = #3*x^2 - 4*x;
end
but the result remains the same
The # doesn't belong there, without it it should work fine. Also I recommend using the pointwise operators, so you can evaluate the function on whole arrays, otherwise they will be interpreted as matrix operations:
function y = df(x)
y = 3*x.^2 - 4*x;
end
The right syntaxis is:
y= #(x)3x^2-4x
You forgot to write the variable "x" betweent braces after "#".
I have three functions and i want two variables to run through all the functions. I tried doing this:
R = rot(mir(sca(P(1,:),P(2,:))));
however i get this error:
Error using mir (line 2)
Not enough input arguments.
Any suggestions?
%rot.m
function rot = rot(x,y)
rot = [ cos(pi/6)*x-sin(pi/6)*y; sin(pi/6)*x+cos(pi/6)*y ];
%mir.m
function mir = mir(x,y)
mir = [x;(-y)];
%sca.m
function sca = sca(x,y)
sca = [2*x;2*y];
You should not be surprised about the error. Function mir expect two parameters (in fact, all of your functions expect that), but you provide only one. Mind you, a matrix is considered one parameter. You can do either of the following to correct the problem:
Redefine mir to accept one parameter and split it inside the function into two separate variables
Redefine sca to return two values:
function [outx, outy] = sca(x, y)
outx = 2 * x;
outy = 2 * y;
and then pass them to mir like so:
[scax, scay] = sca(x, y);
mir(scax, scay);
Obviously, the same needs to be done to function rot as well.
In MATLAB if you have more then one output argument you have to explicitly specify the output variables. By default function always returns one (the first) argument.
In your situation one choice can be to change definitions of your functions in such a way that they receive only one input argument as a matrix. For example:
%mir.m
function mir = mir(xy)
mir = [xy(1,:); -xy(2,:)];
or even easier in this case (you can simplify other functions as well):
function xy = mir(xy)
xy(2,:) = -xy(2,:);
I hope you got the idea.
Then you can run:
R = rot(mir(sca(P(1:2,:))));
If you cannot change your function definitions for some reason, you will have to split the one-line call to three function into 3 lines:
S = sca(P(1,:),P(2,:));
M = mir(S(1,:),S(2,:));
R = rot(M(1,:),M(2,:));
I'm new to Matlab and I'm trying to write custom function in matlab that would take function handle as one of its arguments.
I'm getting this error all the time:
Error using subsindex
Function 'subsindex' is not defined for values of class 'function_handle'.
Trying to debug I performed following test: I run command x = fminbnd(#humps, 0.3, 1). I proceeded as expected - I got result x = 0.6370.
So I created custom function called train and I copied ALL the code of function fminbnd to the file train.m. The only thing that I changed is the name, so that code of functions fminbnd and train is now identical except for the names.
Now I run both functions with the same argument and the custom function throws error while original fminbnd returns correct answer.
Here is the code:
>> x = fminbnd(#humps, 0.3, 1)
x =
0.6370
>> x = train(#humps, 0.3, 1)
Error using subsindex
Function 'subsindex' is not defined for values of class 'function_handle'.
Here is header of function train (everything else is copied from fminbnd):
function [xf,fval,exitflag,output] = train(funfcn,ax,bx,options,varargin)
Where is the problem?
Doing a which train showed me that there is a function in the neural network toolbox of the same name.
/Applications/MATLAB_R2009b.app/toolbox/nnet/nnet/#network/train.m % network method
You may be running the nnet train.m rather than the one you think you're running. Are you in the directory containing your train.m? When I made sure I was in the right directory, I got it to work:
>> which train
/Users/myuserid/train.m
>> x = train(#humps,0.3,1)
x =
0.6370
Maybe you can name your file something else like myfminbnd.m instead?
Instead of duplicating the whole fminbnd function, try:
function varargout = myfminbnd(varargin)
varargout = cell(1,nargout(#fminbnd));
[varargout{:}] = fminbnd(varargin{:});
end
this will work as an "alias" to the existing function:
>> fminbnd(#(x)x.^3-2*x-5, 0, 2)
ans =
0.8165
>> myfminbnd(#(x)x.^3-2*x-5, 0, 2)
ans =
0.8165
(you can get the other output arguments as well)
I have two functions:
function [] = func_one()
S.pb = uicontrol('style','push','unit','pix','posit',[20 20 260 30],
'string','Print Choices','callback',{#func_two,S});
and I have the second function:
function [a] = func_two(varargin)
a = 'alon';
end
I want func_one to return the variable a of func_two. How can I do that please?
I tried:
function [a] = func_one()
But I guess I have to do something with 'callback',{#func_two,S})
Thank you all!
If, as you say, you want func_one to return the value a in func_two then the easiest way to do this without using a callback is:
function [a] = func_one()
S.pb = uicontrol('style','push','unit','pix','posit',[20 20 260 30],
'string','Print Choices');
a = func_two()
The above will allow you to say run a=func_one and a will be the string 'alon'.
If you really really want func_two() to be a callback of your pushbutton, and you want a='alon' to be assigned in the workspace of func_one (the function that calls func_two) then put this in func_two
assignin('caller','a',a)
And if neither is what you want, then maybe you can indicate why you want func_one to return what func_two returns - like the exact interaction you are hoping to have with your GUI and how it differs from what you're actually experiencing.
If you are designing a GUI programmatically, I suggest you use nested functions to share data. Example:
function IncrementExample()
x = 0;
uicontrol('Style','pushbutton', 'String','(0)', ...
'Callback',#callback);
function callback(o,e)
%# you can access the variable x in here
x = x + 1;
%# update button text
set(o, 'String',sprintf('(%d)',x))
drawnow
end
end
I'm using Scilab and I'm trying to make a function like the following:
function p = binary_search(myf,a,b)
The target is to make a binary_search to find such p that: myf(p) = 0 in [a,b].
I want to do something like this:
root = binary_search("x^3 - 10",1,2)
Where the first string is a definition of a function.
The only way I found is defining a function called x3:
function x = x3(p)
x = p^3 - 10;
endfunction
and then, inside binary_search, do something like:
fa = x3(a);
Any ideas?
Thank You!
If you have defined the function f(x) = x^3 - 10 , either using deff('y=f(x)','y=x^3-10') or the regular "function ... endfunction" syntax, then you can simply pass f as an argument: define
function r = binary_search(f,a,b)
% do the binary search here and store the result in r
endfunction
Then you can call
---> binary_search(f, 1, 2)
which works fine in SciLab.
In MATLAB/octave, the interpreter considers " f " as an equivalent for f(), i.e., it would execute the function f without arguments, which will result in an error "x undefined". To avoid this, you have to type an # in front of f:
---> binary_search( #f, 1, 2) %% in MATLAB/octave
Functions in Scilab can be passed as arguments to other functions. Therefore, if you have one function, f:
function y=f(x)
y = x^3 - 10
endfunction
you are free to pass that to another function,
root = binary_search("x^3 - 10",1,2)
deff is simply a way to quickly define a function- usually inline on the interpreter.
Alternatively, you can also pass an expression as a string to a function and have that evaluated using the evstr command:
function p = binary_search(expression, a, b)
evstr expression
//Rest of your code
endfunction
You would implement this on the interpreter thus:
expression = "a^3 - 10"
root = binary_search(expression, 1, 2)
I found a solution:
In the main window (the interpreter), I define the function like:
deff('[y] = square(x)','y=x^2')
Then, I call
bi(square,0,2)
In the function, I just do 'f(x)':
function [x] = bi(f,a,b)
fa = f(a);