フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

having problems troubleshooting this code (vertcat syntax)

1 回表示 (過去 30 日間)
Bailey Owen Banks
Bailey Owen Banks 2020 年 11 月 2 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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