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 ... > 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 ... > "colleen Lischwe" wrote in message ... > > 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 ... > 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 ... > "colleen Lischwe" wrote in message ... > > 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!

Separated by commas
Ex.: root locus, bode

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