Info

此问题已关闭。 请重新打开它进行编辑或回答。

having problems troubleshooting this code (vertcat syntax)

1 次查看(过去 30 天)
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

回答(1 个)

Stephan
Stephan 2020-11-2
编辑:Stephan 2020-11-2
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 个评论
Bailey Owen Banks
Bailey Owen Banks 2020-11-2
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 2020-11-2
Why not post your solution as an answer. May be interesting for others facing the same problem.

此问题已关闭。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by