Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

having problems troubleshooting this code (vertcat syntax)

1 visualización (últimos 30 días)
Bailey Owen Banks
Bailey Owen Banks el 2 de Nov. de 2020
Cerrada: MATLAB Answer Bot el 20 de Ag. de 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

Respuestas (1)

Stephan
Stephan el 2 de Nov. de 2020
Editada: Stephan el 2 de Nov. de 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 comentarios
Bailey Owen Banks
Bailey Owen Banks el 2 de Nov. de 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 el 2 de Nov. de 2020
Why not post your solution as an answer. May be interesting for others facing the same problem.

La pregunta está cerrada.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by