Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
User Defined Function to solve quadratic equation

Subject: User Defined Function to solve quadratic equation

From: colleen Lischwe

Date: 25 Mar, 2012 18:01:12

Message: 1 of 6

I am attempting to create a user defined function that creates the real roots of the quadratic function. I CANNOT use the roots function created by MATLAB.

My parameters are such that if there are no real solutions the function returns "[]", if there is one solution, the function returns one solution and if there are 2 real solutions, the function returns a vector with both possible solutions.

z = b^2 - 4*a*c

if z < 0
y = []
elseif z ==0
y = ?????????????????
else
y = ?????????????????
end

My question is, how do I tell MATLAB to display the solutions if there is one or two solutions rather than displaying the answer to z?

Thanks!

Subject: User Defined Function to solve quadratic equation

From: Roger Stafford

Date: 25 Mar, 2012 18:25:12

Message: 2 of 6

"colleen Lischwe" wrote in message <jknmh7$98r$1@newscl01ah.mathworks.com>...
> I am attempting to create a user defined function that creates the real roots of the quadratic function. I CANNOT use the roots function created by MATLAB.
>
> My parameters are such that if there are no real solutions the function returns "[]", if there is one solution, the function returns one solution and if there are 2 real solutions, the function returns a vector with both possible solutions.
>
> z = b^2 - 4*a*c
>
> if z < 0
> y = []
> elseif z ==0
> y = ?????????????????
> else
> y = ?????????????????
> end
>
> My question is, how do I tell MATLAB to display the solutions if there is one or two solutions rather than displaying the answer to z?
>
> Thanks!
- - - - - - - - -
  I would think you'd want to write a function that allows the quadratic's coefficients to be complex-valued as well as real. When you ask z < 0 you are implicitly assuming a, b, and c are all real.

  Why don't you obtain the two roots using the usual formula and return each of them only if it is real? What could be simpler? Make your return a single vector of 2, 1, or 0 lengths accordingly.

Roger Stafford

Subject: User Defined Function to solve quadratic equation

From: colleen Lischwe

Date: 25 Mar, 2012 20:52:11

Message: 3 of 6


That is a great idea, but for this script I have to follow an algorithm that abides by the parameters of the assignment :( I appreciate your advice, however!






"Roger Stafford" wrote in message <jknnu8$d1r$1@newscl01ah.mathworks.com>...
> "colleen Lischwe" wrote in message <jknmh7$98r$1@newscl01ah.mathworks.com>...
> > I am attempting to create a user defined function that creates the real roots of the quadratic function. I CANNOT use the roots function created by MATLAB.
> >
> > My parameters are such that if there are no real solutions the function returns "[]", if there is one solution, the function returns one solution and if there are 2 real solutions, the function returns a vector with both possible solutions.
> >
> > z = b^2 - 4*a*c
> >
> > if z < 0
> > y = []
> > elseif z ==0
> > y = ?????????????????
> > else
> > y = ?????????????????
> > end
> >
> > My question is, how do I tell MATLAB to display the solutions if there is one or two solutions rather than displaying the answer to z?
> >
> > Thanks!
> - - - - - - - - -
> I would think you'd want to write a function that allows the quadratic's coefficients to be complex-valued as well as real. When you ask z < 0 you are implicitly assuming a, b, and c are all real.
>
> Why don't you obtain the two roots using the usual formula and return each of them only if
it is real? What could be simpler? Make your return a single vector of 2, 1, or 0 lengths accordingly.
>
> Roger Stafford

Subject: User Defined Function to solve quadratic equation

From: Roger Stafford

Date: 25 Mar, 2012 21:18:11

Message: 4 of 6

"colleen Lischwe" wrote in message <jko0hr$7oo$1@newscl01ah.mathworks.com>...
> That is a great idea, but for this script I have to follow an algorithm that abides by the parameters of the assignment :( I appreciate your advice, however!
> ......
- - - - - - - - -
  My advice to you was to not base the decision on the discriminant but on the final roots you obtain (whatever method you use to get them.) Use matlab's 'imag' function. That should surely "abide by the parameters of the assignement."

Roger Stafford

Subject: User Defined Function to solve quadratic equation

From: colleen Lischwe

Date: 25 Mar, 2012 21:36:11

Message: 5 of 6

"Roger Stafford" wrote in message <jko22j$btv$1@newscl01ah.mathworks.com>...
> "colleen Lischwe" wrote in message <jko0hr$7oo$1@newscl01ah.mathworks.com>...
> > That is a great idea, but for this script I have to follow an algorithm that abides by the parameters of the assignment :( I appreciate your advice, however!
> > ......
> - - - - - - - - -
> My advice to you was to not base the decision on the discriminant but on the final roots you obtain (whatever method you use to get them.) Use matlab's 'imag' function. That should surely "abide by the parameters of the assignement."
>
> Roger Stafford

Thanks!

Subject: User Defined Function to solve quadratic equation

From: Steven_Lord

Date: 26 Mar, 2012 13:53:57

Message: 6 of 6



"colleen Lischwe" <colleen.lischwe@yahoo.com> wrote in message
news:jknmh7$98r$1@newscl01ah.mathworks.com...
> I am attempting to create a user defined function that creates the real
> roots of the quadratic function. I CANNOT use the roots function created
> by MATLAB.
>
> My parameters are such that if there are no real solutions the function
> returns "[]", if there is one solution, the function returns one solution
> and if there are 2 real solutions, the function returns a vector with both
> possible solutions.
> z = b^2 - 4*a*c
>
> if z < 0
> y = []
> elseif z ==0
> y = ?????????????????
> else
> y = ?????????????????
> end
> My question is, how do I tell MATLAB to display the solutions if there is
> one or two solutions rather than displaying the answer to z?

The reason this code displays z is because you don't have a semicolon after
it.

z = b^2-4*a*c;

I would create this code as a function that accepts three parameters a, b,
and c and returns one output y. If the parameters of your homework
assignment (I'm guessing, but it looks like a straightforward "introduction
to MATLAB" homework question) require you to INPUT the coefficients, then
create a separate helper script that calls INPUT, passes the values that
were input to your function, and displays the output results. That way you
could reuse your realQuadraticRoots function [the name is up to you] on a
later assignment, even one that's not supposed to display anything in the
Command Window at all.

Now all you need to do is fill in the ???? sections of your code.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us