Solving Quadratic Equations in Matlab: How to Disregard Negative Solutions

  • Context: MATLAB 
  • Thread starter Thread starter Brandt
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 3K views
Brandt
Messages
3
Reaction score
0
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?

Thanks.
 
Physics news on Phys.org
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 positive elements.
 
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 wouldn't 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.
 
Brandt,

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