Where is the error in prompt function in this code?

1 Ansicht (letzte 30 Tage)
Batool Khader
Batool Khader am 5 Apr. 2019
Beantwortet: Walter Roberson am 5 Apr. 2019
for i= 1:4
disp (['enter an angle in degrees']);
x = input(prompt)
y(x) = cos (x);
plot (x,y);
end
y

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 5 Apr. 2019
prompt is not a defined variable. Perhaps you should have used
prompt = 'enter an angle in degrees: ';
Note that your prompt talks about degrees, but cos() works in radians. cosd() works in degrees.
Your code would work (not crash) if the user happened to enter a positive integer, but what if the user happened to enter 0 or negative? y(x) when x is 0 would be a problem.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by