Solving Quadratic Equations in Matlab: How to Disregard Negative Solutions

  • Context: MATLAB 
  • Thread starter Thread starter Brandt
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around solving quadratic equations in Matlab, specifically focusing on how to disregard negative solutions when performing further calculations. Participants share their experiences and challenges related to coding in Matlab, particularly with symbolic objects and array manipulations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant asks how to disregard negative solutions from the results of a quadratic equation in Matlab.
  • Another suggests returning results as an array and writing a subroutine to filter out negative values.
  • A participant shares their attempt to implement a conditional routine to select positive solutions but encounters issues with symbolic objects in Matlab.
  • There is mention of using the 'double' command to convert symbolic results into numerical values, though this leads to further complications with variable naming in function calls.
  • A participant expresses a lack of access to Matlab for testing solutions and invites others with experience to contribute.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to handle negative solutions in Matlab, and multiple competing views and challenges remain unresolved.

Contextual Notes

Participants highlight limitations related to the handling of symbolic objects and the behavior of functions in Matlab, as well as the potential for variable naming conflicts.

Who May Find This Useful

Individuals interested in programming with Matlab, particularly those dealing with symbolic mathematics and quadratic equations, may find this discussion relevant.

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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K