Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

having problems troubleshooting this code (vertcat syntax)

1 vue (au cours des 30 derniers jours)
Bailey Owen Banks
Bailey Owen Banks le 2 Nov 2020
Clôturé : MATLAB Answer Bot le 20 Août 2021
I've been having trouble making this code work, I am trying to make a program that returns the roots of a polynominal, along with if the roots are complex or not based on the discriminant. I keep getting the 'vertcat' syntax. can anyone help point out the error in my code?
function [r] = quadratic(~)
q_fields = {'Coefficient "a"',...
'Coefficient "b"',...
'Coefficient "c"'};
tboxtitle = 'Coefficient Input Menu';
co_values = inputdlg(q_fields,tboxtitle);
a = str2num(co_values{1});
b = str2num(co_values{2});
c = str2num(co_values{3});
x1 = (-1*b + (sqrt(b^2.-(4*a*c))))/(2*a);
x2 = (-1*b - (sqrt(b^2.-(4*a*c))))/(2*a);
x1 = num2str(x1);
x2 = num2str(x2);
d = b^2.- (4*a*c);
d = num2str(d);
if d > 0
r = msgbox(['The roots for your selected coefficients are ',x1,' and ',x2; ...
'There are 2 real roots']);
elseif d == 0
r = msgbox(['The roots for your selected coefficients are ',x1,' and ',x2; ...
'There is 1 real root']);
elseif d < 0
r = msgbox(['The roots for your selected coefficients are ',x1,' and ',x2; ...
'There are 2 imaginary roots']);
end
end

Réponses (1)

Stephan
Stephan le 2 Nov 2020
Modifié(e) : Stephan le 2 Nov 2020
function [r] = quadratic(~)
q_fields = {'Coefficient "a"',...
'Coefficient "b"',...
'Coefficient "c"'};
tboxtitle = 'Coefficient Input Menu';
co_values = inputdlg(q_fields,tboxtitle);
a = str2num(co_values{1});
b = str2num(co_values{2});
c = str2num(co_values{3});
x1 = (-1*b + (sqrt(b^2.-(4*a*c))))/(2*a);
x2 = (-1*b - (sqrt(b^2.-(4*a*c))))/(2*a);
x1 = num2str(x1);
x2 = num2str(x2);
d = b^2.- (4*a*c);
d = num2str(d);
if d > 0
r = msgbox(['The roots for your selected coefficients are ',x1,' and ',x2, ...
'There are 2 real roots']); % ^
elseif d == 0 % |
r = msgbox(['The roots for your selected coefficients are ',x1,' and ',x2, ...
'There is 1 real root']); % ^
elseif d < 0 % |
r = msgbox(['The roots for your selected coefficients are ',x1,' and ',x2, ...
'There are 2 imaginary roots']); % ^
% |
end % |
end % HERE , instead ;
  3 commentaires
Bailey Owen Banks
Bailey Owen Banks le 2 Nov 2020
Thank you Stephen!
I found a workaround shortly after posting - put { } brackets after the msgbox command , as shown. Rinse and repeat for the other conditional statements - but thank you nonetheless!!
r = msgbox({['The roots for your selected coefficients are ' ...
,x1,' and ',x2];'There are 2 real roots'});
Stephan
Stephan le 2 Nov 2020
Why not post your solution as an answer. May be interesting for others facing the same problem.

Cette question est clôturée.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by