# Math Help - Matlab help: User created functions

1. ## 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);

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

Any help would be appreciated!!!!

2. I am unsure what you are needing help with. If you clarify your question I might be able to help.

3. 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.

4. 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

5. 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');

6. Looks to me that you need to change one line

y=a.*x.^2+b.*x+c

Just because multiplication may need to be told to multiply component wise. I could be wrong though. I say run it and see if it comes out right.

7. Originally Posted by lvleph
Looks to me that you need to change one line

y=a.*x.^2+b.*x+c

Just because multiplication may need to be told to multiply component wise. I could be wrong though. I say run it and see if it comes out right.
Hey lvleph,

Thank you for your prompt response.
I've tried both solutions and they both output the same parabola. Seems it's OK.

8. etacarinae

I cannot see where you have first defined what a,b,c are before using them?