필터 지우기
필터 지우기

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