I am unsure what you are needing help with. If you clarify your question I might be able to help.
Can someone please help me with this? I need this code to begin the more complicated step and I can't figure it out. I don't understand how to define the function and use it later, while also asking for variables to be entered.
Write a program (without user created functions) that asks the user to enter the coefficients of a (one) quadratic equation and compute the real roots. Appropriate error checking should be included to ensure the first coefficient is not equal to zero and that the determinate is not less than zero. Print the coefficients and real roots to the screen with appropriate labels.
I have this so far as my function.
function [x1, x2] = quadratic_equation (a,b,c)
x1=(-b+sqrt(b^2-4*a*c))/(2*a);
x2=(-b-sqrt(b^2-4*a*c))/(2*a);
My code was this but its obviously way off:
a=input ('Enter the first coefficient - cannot be zero: ');
b=input ('Enter the second coefficient: ');
c=input ('Enter the third coefficient: ');
function [x1, x2] = quadratic_equation (a,b,c)
x1=(-b+sqrt(b^2-4*a*c))/(2*a);
x2=(-b-sqrt(b^2-4*a*c))/(2*a);
end
fprintf('roots = %d\n', quadratic_equation(x1,x2);
Any help would be appreciated!!!!
This is what I am trying to do:
Write a program (without user created functions) that asks the user to enter the coefficients of a (one) quadratic equation and compute the real roots. Appropriate error checking should be included to ensure the first coefficient is not equal to zero and that the determinate is not less than zero. Print the coefficients and real roots to the screen with appropriate labels.
Well, I don't see anything wrong with the function that you have except that you have not checked if the first term is 0 and you have not checked the descriminate.
Code:if(a==0) print('a was zero, did not calculate root.'); return; elseif(b^2 - 4*a*c < 0) print('Roots are not real.'); return; end
Hello,
I'm writing the exact same program, but in Octave, and plotting the quadratic parabola as an additional feature. I'm wondering whether this code is correct?
Code:x = input('Enter vectors for x'); y= a*x.^2+b*x+c; figure(1); %Allocate a figure window clf; %Clear the figure window plot(x,y); xlabel('x axis'); ylabel('y axis'); title('quadratic plot');