MATLAB Solving Quadratic Equations in Matlab: How to Disregard Negative Solutions

  • Thread starter Thread starter Brandt
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
To disregard negative solutions when solving quadratic equations in Matlab, users can create a function to filter out negative values from the results. The challenge arises when the solutions are symbolic objects, which complicates the use of standard array operations. Converting symbolic results to numerical values using the 'double' command can help, but may lead to errors when called within a function due to variable conflicts. Users are encouraged to share insights or alternative methods for handling these issues. The discussion highlights the need for effective handling of symbolic mathematics in Matlab to streamline calculations.
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 postive 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.
 

Similar threads

Replies
5
Views
3K
Replies
2
Views
3K
Replies
5
Views
3K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
1
Views
4K
Replies
4
Views
1K
Back
Top