I don't get the desired output when running the following code. The output generated is shown in the image.

2 ビュー (過去 30 日間)
The task is to generate the roots of a quadratic equation and the value of the variables are entered by the user. I have used the following code:
%%findingRoots.m
border=['*************************************************'];
intro=['Quadratic Solver for ax^2+bx+c=0'];
disp(border);
disp(intro);
a=input('Please enter a: ');
b=input('Please enter b: ');
c=input('Please enter c: ');
z1=(-b+(b^2-(4*a*c))^0.5)/(2*a);
z2=(-b-(b^2-(4*a*c))^0.5)/(2*a);
root=['The roots are: '];
root1=['z1 = ', z1];
root2=['z2 = ', z2];
disp(root);
disp(root1);
disp(root2);
and I get the response as shown in the image.

回答 (1 件)

Zoltán Csáti
Zoltán Csáti 2014 年 10 月 27 日
I do not see the image (perhaps you did not attach it), but the error in your code is the following. You want to display a string so first you must convert z1 and z2 to strings, like this:
root1=['z1 = ', num2str(z1)];
root2=['z2 = ', num2str(z2)];
Then it will work.

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by