Undefined function or variable when using fprintf? I have already defined what the variable is however when i run the code it doesn't work.

16 vues (au cours des 30 derniers jours)
The assignment was to use the given equations to write a script solving a cubic function. I have to use fprintf to display a sentence saying the equation and the roots that are associated with it. Below is the code I have so far, and it says that the error appears in line 34 with the variable L. I defined the variable L above, so I don't know if it is formatted wrong somewhere else causing it not to work. Any help would be greatly appreciated.
% This script determines the roots of a cubic equation.
y = input('input the x^3 value here');
x = input('input the x^2 value here');
w = input('input the x value here');
z = input('input the constant value here');
A = x/y;
B = w/y;
C = z/y;
Q = (3*B-A^2)/9;
R = (9*A*B-27*C-2*A^3)/54;
T = acosd(R/sqrt(-Q^3));
D = Q^3+R^2;
switch D
case 1
if D>0
fprintf('The equation %g x^3+ %g x^2 +%g x+ %g has complex roots.\n', y, x, w, z)
end
case 2
if D == 0
L = (2 .* nthroot(R,3) - (A/3));
M = (-nthroot(R,3) - (A/3));
N = M;
fprintf('The roots of the equation %g x^3+ %g x^2 +%g x+ %g are %g, %g, and %g.\n', y, x, w, z, L , M, N)
end
case 3
if D < 0
L = 2* (sqrt(-Q).*cosd(T./3))-A/3;
M = 2* (sqrt(-Q).*cosd(T./3 + 120))-A/3;
N = 2* (sqrt(-Q).*cosd(T./3+240))-A/3;
fprintf('The roots of the equation %g x^3+ %g x^2 +%g x+ %g are %g, %g, and %g.\n', y, x, w, z, L , M, N)
end
end
  2 commentaires
Jan
Jan le 6 Mar 2017
The posted code has 32 lines only, therefore we cannot guess, which line causes the error. Please post a copy of the complete message and mention which line fails. Thanks.
dpb
dpb le 6 Mar 2017
This isn't the original code that had the subject-line error, Jan, hence my comment below to OP to not edit the code without noting what changes are being made.
The original had different error in logical construct such that the three fprintf statements were all executed irregardless of D and L isn't defined for D>0 which caused the error. But, as noted, that code isn't around any longer and the above has different issues...

Connectez-vous pour commenter.

Réponses (1)

dpb
dpb le 5 Mar 2017
Modifié(e) : dpb le 6 Mar 2017
L is undefined for Case D>0...and, as written the last three IF statements will all be executed every time irrespective of the value of D--clearly not what you're intending.
UPDATE POST ORIGINAL QUESTION MODIFICATIONS
Don't change the question/code/problem w/o making comments thereto...
Now you've got
switch D
case 1
...
case 2
...
which won't do what you want because in general D will not be integer and certainly won't be 1,2,3 for the three cases.
ADDENDUM
See the "More About" section for
doc switch
"A case_expression cannot include relational operators such as < or > for comparison against the switch_expression. To test for inequality, use if, elseif, else statements."
I think it's a shortcoming in implementation in Matlab that it isn't possible to write conditionals similar to (say) Visual Basic, but "that's the way it is"...in VB-like Matlab (transliterated from SELECT CASE to SWITCH) one could write
switch D
case is < 0
...
case 0
...
etc., but just not possible as the language now exists, imo, unfortunately, although you can, of course write it with IF...ELSEIF...END constructs albeit a little more verbosely.*(+)*
And, of course, your current version has the start of that buried underneath the switch construct; it should be pretty evident how to make the transition...
(+) NB:
Being as your cases are mutually exclusive, it's also possible to write as three standalone IF blocks although the ELSEIF should short-circuit the other tests whereas all three tests would have to be made in the independent-block construct form. (Just as a pedagogical point; not suggesting to do this).

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by