Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple Question about Matlab

  1. Nov 21, 2005 #1
    Hi Guys,
    I just have a simple question about Matlab. When solving a quadratic equation two answers will be produced. If one is Negative what command can I use in Matlab to disregard it and continue further calculations with just the positive one?

  2. jcsd
  3. Nov 22, 2005 #2


    User Avatar
    Science Advisor
    Homework Helper

    The result should be returned as an array and you can access individual elements to determine the sign of each one. Write a subroutine/function to test the elements of the matrix and return a matrix with only postive elements.
  4. Nov 29, 2005 #3
    Thanks Tide. I tried what you said, writing a little if else routine to give me only the positive answer, put that led to another problem. It wouldnt work because the answer of the quadratic is a sym object and the routine will only work for an array. Do you know how i can convert it or find a way around this? I thought I would send you part of the code that i wrote:

    >> h = 500;% Input Value
    >> V = 55;% Input Value
    >> syms t;
    >> T = @(h,V)((V*sin(0))*t - 0.5*9.8*t^2 + h );
    >> T(h,V);
    >> T = ans;
    >> solve(T);
    >> T = ans
    >> if ans(1,1)> 0 & ans(2,1)>0
    >> T = ans
    >> elseif ans(1,1)< 0 & ans(2,1)>0
    >> T = ans(2,1)
    >> else T= ans(1,1)
    >> end

    Let me know what you think. Thanks for the help.
  5. Nov 30, 2005 #4


    User Avatar
    Science Advisor
    Homework Helper


    I don't have current access to MatLab so I can't fiddle with it to try and resolve the issue. I do have a clone with sparse documentation but I'll see if I can dig anything up. Any current ML users - feel free to jump in!
  6. Dec 5, 2005 #5
    Oh ok, well thanks Tide. I did find a way to convert from into an actual number, that is with the double command. however, this proved inconsequential as Matlab refuses to use this programming block when I call it up as a function giving me an error message that I have used the variable t (in code above) as a command/function. It seems to ignore the fact that I defined it as an symbolic object before. Funnily enough, when I run the routine on its own It works fine. If anybody has any ideas please let me know. Thanks.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Simple Question about Matlab