# Matlab help: User created functions

• Mar 24th 2010, 11:50 AM
roryman987
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!!!!
• Mar 24th 2010, 12:38 PM
lvleph
I am unsure what you are needing help with. If you clarify your question I might be able to help.
• Mar 24th 2010, 01:30 PM
roryman987
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.
• Mar 24th 2010, 01:46 PM
lvleph
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```
• Jul 7th 2010, 01:28 PM
etacarinae
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');```
• Jul 7th 2010, 01:59 PM
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.
• Jul 7th 2010, 02:17 PM
etacarinae
Quote:

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. :)
• Jul 7th 2010, 07:21 PM
elbarto
etacarinae

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