matlab error, Undefined function or variable 'funczaro01'.

1 回表示 (過去 30 日間)
Erick Miranda
Erick Miranda 2014 年 6 月 16 日
回答済み: Erick Miranda 2014 年 7 月 4 日
Hi, Iam a new user of matlab, hlep me , please. I have the following code in an .M file:
function y=funczero01
%Finding the zeros of the function below
fplot('funczero01',[-1.5 1.5]);grid
x1=fzero('funczero01',-0.2);
x2=fzero('funczero01',0.3);
fprintf('the roots(zeros)of this function are r1=%3.4f',x1);%this command is explained `
below
fprintf('and r2=%3.4f\n',x2)
I get the following error:
funczero01 ??? Undefined function or variable 'x'.
What can I do?, I tried to fix it using: syms x, but I get the following error:
funczero01 ??? Error using ==> funczero01 Too many input arguments.

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 6 月 16 日
編集済み: Geoff Hayes 2014 年 6 月 16 日
Erick - your above code should be formatted to make it more readable or it can be attached to your question.
The function definition seems to be something like
function y=funczero01
%Finding the zeros of the function below
fplot('funczero01',[-1.5 1.5]);
grid
x1=fzero('funczero01',-0.2);
x2=fzero('funczero01',0.3);
% etc.
One problem is the use of fplot and and fzero: both functions are being passed handles to your main function, funczero01. If the function signature were set correctly, this will cause the function to recurse until the maximum recursion limit is reached. You don't want to call a function, only to pass itself to another function without including some sort of "stop" case (though I'm not entirely sure what it is you wish to do here).
But this recursion is not yet occurring because of the error of too many input arguments. Note that for fplot (see http://www.mathworks.com/help/matlab/ref/fplot.html) the function handle is to a function of the form y = f(x), where x is a vector whose range specifies the limits, and y is a vector the same size as x and contains the function's value at the points in x. So while your function has an output argument of y (which is never set) it is missing an input argument x. So when fplot tries to pass an input value to your function, the above error is observed. (And you can see this too if in the Command Window you type funczero01(34). For fzero (see http://www.mathworks.com/help/matlab/ref/fzero.html?searchHighlight=fzero#inputarg_fun), the function handle must accept a scalar x and return as scalar y.
The signature for your function must be changed to the following
function y=funczero01(x)
With that fixed, the program will still "crash" when the maximum recursion limit is reached. You must decide what this function is supposed to do for each input it receives. How is y determined from x?
Next you must create a new m file which calls this function
function testingFplotFzero
%Finding the zeros of the function below
fplot('funczero01',[-1.5 1.5]);
grid
x1=fzero('funczero01',-0.2);
x2=fzero('funczero01',0.3);
fprintf('the roots(zeros)of this function are r1=%3.4f',x1);
%this command is explained ` below
fprintf('and r2=%3.4f\n',x2)
end
and modify the original file with the logic for determining y from x
function [y] = funczero01(x)
% initialize y
y = 0
% do something with x to get y
end
Try it out and see what happens!

その他の回答 (1 件)

Erick Miranda
Erick Miranda 2014 年 7 月 4 日
Sorry, sorry, I missed a line
function y=funczero01 %Finding the zeros of the function below y=1/((x-0.1)^2+0.01)-1/((x-1.2)^2+0.04)-10; fplot('funczero01',[-1.5 1.5]);grid x1=fzero('funczero01',-0.2); x2=fzero('funczero01',0.3); fprintf('the roots(zeros)of this function are r1=%3.4f',x1);%this command is explained ` below fprintf('and r2=%3.4f\n',x2)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by