Solving Quadratic Equations in Matlab: How to Disregard Negative Solutions

  • MATLAB
  • Thread starter Brandt
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses the issue of disregarding negative answers when solving a quadratic equation in Matlab. The suggested solution is to use a subroutine/function to test the elements of the matrix and return a matrix with only positive elements. However, there is a problem with converting the answer from a symbolic object to an array. The conversation ends with a request for help on how to resolve this issue.
  • #1
Brandt
3
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
  • #2
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.
 
  • #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 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.
 
  • #4
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!
 
  • #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.
 

1. What is Matlab and what is it used for?

Matlab is a programming platform that is commonly used for numerical computations, data analysis, and visualization. It also has built-in tools for solving mathematical equations, creating graphs and plots, and developing algorithms.

2. How do I get started with using Matlab?

To use Matlab, you will need to download and install the software on your computer. You can either purchase a license or use the free trial version. Once installed, you can open the program and start writing code in the editor or using the graphical user interface (GUI).

3. Can Matlab be used for data analysis and visualization?

Yes, Matlab has powerful tools for data analysis and visualization. It has built-in functions for importing, manipulating, and analyzing data, as well as creating various types of plots and graphs to display the data.

4. Is Matlab difficult to learn?

The difficulty of learning Matlab may vary depending on your background and experience with programming. However, with the vast amount of resources and documentation available, it is relatively easy to get started and learn the basics. Practice and experimentation are key to mastering Matlab.

5. Can I use Matlab for machine learning and artificial intelligence?

Yes, Matlab has a deep learning toolbox that provides tools and functions for developing and implementing machine learning and artificial intelligence algorithms. It also has built-in functions for data preprocessing, feature extraction, and model training and evaluation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
827
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
986
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
Back
Top