Verfasst am: 07.11.2016, 17:19
Titel: Error using keyboard - Undefined function or variable 'x'.
Hallo,
ich bin ein blutiger Matlab Anfänger und komme gerade bei dieser Fehlermeldung nicht weiter: "Error using keyboard - Undefined function or variable 'x'."
Was könnte das bedeuten? Der Code sieht folgt aus:
Error using keyboard - Undefined function or variable 'x'.
Code:
The Newton-Raphson Method
First, write a script file itermenu.m, containing the interactive men
u:
disp( Please choose:) disp( To zoom in....................1) disp( To set the initial value...... 2) disp( To quit........................3)
Then, in a separate file mynewton.m, write the main program:
% Newton-Raphson method to detect the roots of a function y
tol = 0.00001; % set the tolerance disp(Give the function y(x) and its derivative as string va
riables: ) disp(for example, y = x - sin(x) - c) disp(and dydx = 1 - cos(x)) disp(then leave the inputmode - type return, then <ent
er>) keyboard disp(Give the domain, then type <enter>)
xmin = input(x min = )
xmax = input(x max = )
xx = xmin: (xmax-xmin)/100: xmax;
3
z = zeros(1, 100);
for k = 1:101
x = xx(k);
yy = eval(y);
z(k) = yy;
end plot(xx, z) grid title(Plot of the function y) xlabel(x) ylabel(y(x)) pause
real_zeros = [];
itermenu
answer = input(Please choose 1, 2 or 3 ) while answer ~= 3 if answer == 1
xmin = input(x min = )
xmax = input(x max = )
xstep = (xmax - xmin)/100;
xx = xmin: xstep: xmax;
z = zeros(1, 100);
for k = 1: 101
x = xx(k);
yy = eval(y);
z(k) = yy;
end plot(xx, z) grid xlabel(x) ylabel(y) pause
itermenu
answer = input(Your choice? ) elseif answer == 2 disp(Select the initial value with the mouse ) [ x, yg ] = ginput(1) for l = 1:100% its the letter L
x0 = x;
x = x0 - eval(y)/eval(dydx)% the Newton formula ifabs(x - x0) < tol
break end end if(l == 100)&(abs(x - x0) >= tol) disp(the sequence does not converge) else
real_zeros = [ real_zeros x ];
end 4
itermenu
answer = input(Your new choice? ) else if answer ~= 3 disp(Wrong input, please try again)
itermenu
answer = input(Your new choice? ) end end
en
In dem Code taucht hier der
keyboard
Befehl auf. Offenbar tippst Du hier einen Ausdruck ein, in dem die Variable "x" vorkommt. Da diese Ausdruck aber direkt abgearbeitet wird und "x" nicht definiert ist, kommt es zu dieser Fehlermeldung.
Eigentlich soll wohl wahrscheinlich ein String eingegeben werden. Dann wäre
input
aber besser als
keyboard
.
Der Code ist ein gutes Beispiel dafür, wie man nicht programmieren sollte. Über "eval" findest Du hunderte Thread in diesem Forum, eine Suche lohnt sich also.
Gruß, Jan
Einstellungen und Berechtigungen
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.