Results 1 to 4 of 4

Thread: Avoiding having ans returned in a Matlab's function

  1. #1
    Newbie
    Joined
    Jul 2010
    Posts
    3

    Exclamation Avoiding having ans returned in a Matlab's function

    Hi!
    I did a simple function to solve quadratic equation. Everytime this function displays x1 , x2 and ANS. I don't want to view ANS because it's only the first variable, and I want to show both.
    How can I do this?
    This is the function:

    function[x1,x2]=eqgrado2(a,b,c)
    %--------------------------------------------------------
    % la function dà le soluzioni di un'equazione di secondo grado
    %
    %parametri in ingresso:
    % a coefficiente del termine di secondo grado
    % b coefficiente del termine di primo grado
    % c termine noto
    %
    %parametri in uscita:
    % x1 e x2 radici dell'equazione
    %----------------------------------------------------------
    delta=b^2-4*a*c;
    if ((delta<0)&(a>0))|((delta>0)&(a<0))
    disp('non ci sono soluzioni reali')
    return
    elseif delta==0
    disp('ci sono 2 soluzioni reali e coincidenti')
    x1=-b/2*a
    x2=x1
    else
    disp('ci sono due soluzioni reali e distinte')
    x1=(-b-sqrt(delta))/2*a
    x2=(-b+sqrt(delta))/2*a
    end


    p.s. I'm sorry for my english but...I'm italian...
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Bar0n janvdl's Avatar
    Joined
    Apr 2007
    From
    South Africa
    Posts
    1,632
    Thanks
    9
    I changed your code a bit.

    Code:
    function eqgrado2(a,b,c)
        delta = (b^2-4*a*c);
        if (delta < 0)
            disp('non ci sono soluzioni reali')
        elseif (delta == 0)
            disp('ci sono 2 soluzioni reali e coincidenti')    
            x1=-b/2*a;
            x2=x1;
        else
            disp('ci sono due soluzioni reali e distinte')
            x1=(-b-sqrt(delta))/2*a;
            x2=(-b+sqrt(delta))/2*a;
        end
        
        disp(sprintf('x1 = %d, x2 = %d', x1, x2));
    end
    Basically what got it working was this:

    Code:
    function eqgrado2(a,b,c)
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Jul 2010
    Posts
    3
    I changed it as you tell me but there is still ANS... =(
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    3
    Code:
    function x=eqgrado2(a,b,c)
    %--------------------------------------------------------
    % la function dà le soluzioni di un'equazione di secondo grado
    %
    %parametri in ingresso:
    % a coefficiente del termine di secondo grado
    % b coefficiente del termine di primo grado
    % c termine noto
    %
    %parametri in uscita:
    % x1 e x2 radici dell'equazione 
    %----------------------------------------------------------
    delta=b^2-4*a*c;
    if ((delta<0)&(a>0))|((delta>0)&(a<0))
      disp('non ci sono soluzioni reali')
      return 
    elseif delta==0
      disp('ci sono 2 soluzioni reali e coincidenti')
      x1=-b/2*a
      x2=x1
    else 
      disp('ci sono due soluzioni reali e distinte')
      x1=(-b-sqrt(delta))/2*a
      x2=(-b+sqrt(delta))/2*a
    end
    x=[x1,x2];
    Will return [x1,x2], if you dont want it displayed call with:

    Code:
    eqgrado2(a,b,c);
    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Avoiding Large Number of Terms in Equation Expansion
    Posted in the Advanced Math Topics Forum
    Replies: 0
    Last Post: April 28th 2011, 11:59 AM
  2. [SOLVED] Avoiding elliptic sine/cosine/etc
    Posted in the Advanced Math Topics Forum
    Replies: 3
    Last Post: May 25th 2010, 05:29 AM
  3. [SOLVED] Avoiding ratios in gauss-jordan elimination?
    Posted in the Advanced Algebra Forum
    Replies: 3
    Last Post: February 12th 2010, 03:05 PM
  4. Avoiding having ans returned (matlab)
    Posted in the Math Software Forum
    Replies: 2
    Last Post: October 27th 2009, 07:47 AM

Search Tags


/mathhelpforum @mathhelpforum