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

2 views (last 30 days)
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.

Answers (1)

Zoltán Csáti
Zoltán Csáti on 27 Oct 2014
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.

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!