WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Error using keyboard - Undefined function or variable 'x'.

 

spider89
Forum-Newbie

Forum-Newbie


Beiträge: 1
Anmeldedatum: 07.11.16
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 07.11.2016, 17:19     Titel: Error using keyboard - Undefined function or variable 'x'.
  Antworten mit Zitat      
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 input mode - 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)
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      % it’s the letter L
x0 = x;
x = x0 - eval(y)/eval(dydx)   % the Newton formula
if abs(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



Viele Grüße,
spider
Private Nachricht senden Benutzer-Profile anzeigen


Jan S
Moderator

Moderator


Beiträge: 11.057
Anmeldedatum: 08.07.10
Wohnort: Heidelberg
Version: 2009a, 2016b
     Beitrag Verfasst am: 07.11.2016, 18:19     Titel: Re: Error using keyboard - Undefined function or variable 'x
  Antworten mit Zitat      
Hallo spider89,

Codes, die EVAL verwenden, betrachte ich grundsätzlich als falsch. Wie kommst Du als Anfänger dazu, so ein fieses Programm zu verwenden?

Zitat:
Code:
disp(’then leave the input mode - type ’’return’’, then <ent
er>’)
keyboard

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
Private Nachricht senden Benutzer-Profile anzeigen
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

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
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2025 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

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.