Matlab help: User created functions
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!!!!