MATLAB not enough input argument error - matlab

Maybe i'm missing something obvious, but i'm just starting matlab so...
I have created many user defined function and use them to solve another function.
each function works when tested separately, but when I try test the bigger function, i get this message:
Error using F1 (line 2)
Not enough input arguments.
Error in run (line 5)
[X,Y]=NewrRaph2(F1,F2,d1x,d1y,d2x,d2y,1,1,iter)
my function F1 is:
function [F1]=F1(x,y)
F1=4*x^2+y^3+28;
and my function Newraph2 is:
function [X,Y]=NewrRaph2(F1,F2,d1x,d1y,d2x,d2y,x,y,iter)
x=x;
y=y;
for n=1:iter
deltax=((-F1(x,y)*d2y(y))+(F2(x,y)*d1y(y)))/Jacob(dix(x),d1y(y),d2x(x),d2y(y));
deltay=((-F2(x,y)*d1x(x))+(F1(x,y)*d2x(x)))/Jacob(d1x(x),d1y(y),d2x(x),d2y(y));
x=deltax+x;
y=deltay+y;
end
X=x;
Y=y;
also, to test my function I use this script:
clear, clc
x=input('valeur de x ')
y=input('valeur de y ')
iter=input('valeur de iter: ');
[X,Y]=NewrRaph2(F1,F2,d1x,d1y,d2x,d2y,x,y,iter)
But I don't understand what I did wrong.

Related

MATLAB Optimization toolbox example

https://www.mathworks.com/help/optim/examples/banana-function-minimization.html
fun = #(x)(100*(x(2) - x(1)^2)^2 + (1 - x(1))^2);
options = optimset('OutputFcn',#bananaout,'Display','off');
x0 = [-1.9,2];
[x,fval,eflag,output] = fminsearch(fun,x0,options);
title 'Rosenbrock solution via fminsearch'
Fcount = output.funcCount;
disp(['Number of function evaluations for fminsearch was ',num2str(Fcount)])
disp(['Number of solver iterations for fminsearch was ',num2str(output.iterations)])
What is #bananaout here?
This is giving me the following error,
??? Error using ==> feval
Attempt to execute SCRIPT bananaout as a function:
C:\Users\admin\Desktop\bananaout.m
Error in ==> callAllOptimOutputFcns at 12
stop(i) = feval(OutputFcn{i},xOutputfcn,optimValues,state,varargin{:});
Error in ==> fminsearch>callOutputAndPlotFcns at 464
stop = callAllOptimOutputFcns(outputfcn,xOutputfcn,optimValues,state,varargin{:})
|| stop;
Error in ==> fminsearch at 199
[xOutputfcn, optimValues, stop] =
callOutputAndPlotFcns(outputfcn,plotfcns,v(:,1),xOutputfcn,'init',itercount, ...
Error in ==> test_optim at 9
[x,fval,eflag,output] = fminsearch(fun,x0,options)
As per the doc, Output Functions are called by the optimizer at every time step, enabling you to do things like plot the progress of the optimization.
In your case you get an error because bananaout seems to be a script when it needs to be a function (with specific inputs - see the doc for their details). Did you happen to save the example code in a script called bananaout? If so, rename the script.
You can see a list of all m-code that you have that are called bananaout by executing the following:
>> which bananaout -all
One of them will be the function that the example should be calling, while another will be the one that you have created and need to rename/remove.

Not enough input arguments when calling GA toolbox

I called Matlab's genetic algorithm solver, ga, in the following way:
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
where theta is a 26-by-1 column vector that needs to be optimized.
So in the main function, it goes like this:
clc
clear
global var1 var2...
load ('abcd.mat')
theta0=[1 2 3....];
LB=[26-by-1 row vector];
UB=[26-by-1 row vector];
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
The fitness function, smmobj, is defined as:
function [obj]=smmobj(theta)
global var1 var2...
But when I run it, it always says:
Error using smmobj (line 4)
Not enough input arguments.
Error in SMMga (line 32)
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
But I run the fitness function by itself, it works.
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
will throw that error because you are essentially calling the function smmobj (with no parameters) instead of passing your ga function a function handle to your objective function. You do this using the # symbol so
[theta,fval,exitflag] = ga(#smmobj,26,[],[],[],[],LB,UB,[],[]);
Incidentally, I would recommend that you do not use global in order to pass the extra parameters of var1 and var2 to your objective function but rather use anonymous functions:
[theta,fval,exitflag] = ga(#(x)(smmobj(x,var1,var2)),26,[],[],[],[],LB,UB,[],[]);
and then change smmobj's definition to
function [obj]=smmobj(theta,var1,var2)

fzero: Too many output arguments

To learn how to work with fzero() I tried this code:
function equation(x)
k=(96-x)/6;
end
and then:
x0=4;
x=fzero('equation',x0)
The error is:
??? Error using ==> fzero at 307
FZERO cannot continue because user supplied function_handle ==> equation
failed with the error below.
Too many output arguments.
fzerois expecting a return value from your equation so (internally) it is trying to assign something to the output of that function. If I try
result = equation(42);
I get the same error message
Error using equation
Too many output arguments.
Just change your function signature to
function [k] = equation(x)
to indicate that k is the output of this function.
Try to supply the function argument as handle:
x = fzero(#equation,x0)

Invoking a function from another which has the same number of inputs and outputs

I want to call a function in Matlab using another one, which has the same number of inputs and outputs. In fact, those inputs and outputs have the same name.
Example:
function [a,b] = gettwo(matrix,string,varargin)
[a,b] = getone(matrix,string,varargin{:});
end
This code produces the following error:
Error in getone(line 3)
aux = 'matrix(varargin{:})';
Output argument "b" (and maybe others) not assigned during
call to "C:\Users\baister\Documents\MATLAB\soft\getone.m>getone".
Error in results (line 4)
[a,b] = getone(matrix,string,varargin{:});
How should I wrap getone?
(The definitive function will have more lines than those shown in this post.)
Thanks.
The general wrapping for variable number of outputs should work like this:
function [varargout] = gettwo(matrix,string,varargin)
[varargout{1:nargout}] = getone(matrix,string,varargin{:});
end
You'll get the same error as above though, in case you do
[a,b] = gettwo(...);
and getone returns only 1 argument.

How to write a function that does not throw a "wrong number of arguments" error

I am trying to write a minimal function that can be called with a variable number of arguments but that will not throw a wrong number of arguments error if miscalled.
Here is where I start from :
function varargout=fname(varargin)
% FNAME
% Usage: output=fname(input)
% Arguments check
if(nargin~=1 || nargout~=1)
disp('Function fname requires one input argument');
disp('and one output argument');
disp('Try `help fname`');
varargout(1:nargout)={0};
return;
end
input=varargin{1};
output=input;
varargout(1)={output};
end
However this does not work as I would like it to. Is there a way to write a function that :
never throw a "wrong number of arguments" error (so that the rest of the execution can continue)
accepts variable number of input and output arguments and checks them inside the function
(maybe more tricky) if the number of input / output arguments is not correct, does not replace the value of the provided output arguments (so that any misplaced call does not erase the previous value of the output argument)
I am open to any suggestions / other methods.
Thank you for your help.
UPDATE: thanks to #Amro for his answer, I guess what I miss here is either a call by address of reference for Matlab functions or a way to interrupt a function without returning anything and without stopping the rest of the execution.
Here is one way to implement your function:
function varargout = fname(input,varargin)
%# FNAME
%# Usage: output=fname(input)
%%# INPUT
if nargin<1
varargout(1:nargout) = {[]};
warning('Not enough input arguments.'), return
end
if ~isempty(varargin)
warning('Too many input arguments.')
end
%%# YOUR CODE: manipulate input, and compute output
output = input;
%%# OUTPUT
varargout{1} = output;
if nargout>1
warning('Too many output arguments.')
varargout(2:nargout) = {[]};
end
end
Obviously you can customize the warning messages to your liking...
Also, if you want your function to simply print the message instead of issuing warnings, replace all WARNING calls with simple DISP function calls.
Examples of function call:
fname()
fname(1)
fname(1,2)
x = fname()
x = fname(1)
x = fname(1,2)
[x,y] = fname()
[x,y] = fname(1)
[x,y] = fname(1,2)
The above calls execute as expected (showing warning messages when applicable). One caveat though, in the last three calls, if the variable y already existed in the workspace prior to the calls, it would be overwritten by the empty value y=[] in each...
If I understand your question correctly, then the answer is no. If a caller calls a function like this:
[a, b, c] = fname('foo');
then fname is required to return (at least) three outputs. There's no way to tell MATLAB that it should leave b and c alone if fname only returns one output.