Results 1 to 8 of 8

Math Help - Matlab help: User created functions

  1. #1
    Newbie
    Joined
    Feb 2010
    Posts
    5

    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!!!!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Senior Member
    Joined
    Mar 2009
    Posts
    378
    I am unsure what you are needing help with. If you clarify your question I might be able to help.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Feb 2010
    Posts
    5
    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.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Senior Member
    Joined
    Mar 2009
    Posts
    378
    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
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Jun 2010
    Posts
    2

    Exclamation

    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');
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Senior Member
    Joined
    Mar 2009
    Posts
    378
    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.
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Jun 2010
    Posts
    2
    Quote Originally Posted by lvleph View Post
    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.
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    etacarinae

    I cannot see where you have first defined what a,b,c are before using them?
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Scaling in already created eq's in mathtype
    Posted in the LaTeX Help Forum
    Replies: 1
    Last Post: February 8th 2010, 08:45 AM
  2. Roots to exponents, self created expressions
    Posted in the Algebra Forum
    Replies: 2
    Last Post: June 13th 2009, 05:59 PM
  3. Dealing with user-defined functions
    Posted in the Calculators Forum
    Replies: 0
    Last Post: October 27th 2008, 02:25 PM
  4. what's wrong with my pdf created by texshop?
    Posted in the LaTeX Help Forum
    Replies: 9
    Last Post: June 9th 2008, 01:13 PM
  5. Area of triangle created from 3 planes
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: April 14th 2008, 07:27 AM

Search Tags


/mathhelpforum @mathhelpforum