Help with quadriatic formula?

1 view (last 30 days)
MAB
MAB on 12 Sep 2013
Commented: dpb on 17 Oct 2013
Hi everyone,
We are to create a function using the editor to solve a quadratic formula. The hard part is the user is to be able to input just the equation ie: Ax^2+Bx+C=0 (not the values of ABC). (Below is what I have so far)
____***_*%The user input the formula they want solved_____
function [xpos, xneg] = Roots(equation)
%Quadratic equation format is = Ax^2 + Bx + C = 0
A = strtok(equation, 'x');
B =
C =
%This is the discriminant
D = B .^2 - 4 .* A .* C;
%These are the equation to find the positive and negative values of x
xpos = (-B + sqrt(D)) ./ (2 .* A)
xneg = (-B - sqrt(D)) ./ (2 .* A)
end
  3 Comments
Daniel Shub
Daniel Shub on 17 Sep 2013
There are lots of parsers for quadratic equations based on regular expressions. This might be easier than using strtok
dpb
dpb on 17 Sep 2013
Oh, indeed! The OPs instructor is, however, apparently not amenable to alternate solutions than those suggested based on his comments later on.

Sign in to comment.

Accepted Answer

dpb
dpb on 12 Sep 2013
You showed enough I'll do more than normally would on first response...
s=inputdlg('Enter quadratic: ','Quadratic Solver Input',1,{'ax^2+bx+c'});
  8 Comments
MAB
MAB on 17 Oct 2013
OK, Im back again because I realized that if there is no number in front the x, my function will not work. So I am looking for a way to use the strtok without using an if statement to determine that there is a 1 whether it be negative or positive as the coefficient.
ie: -x^2 - x +10 = 0
I want to be able to pull out a -1 for A and B.
dpb
dpb on 17 Oct 2013
The form doesn't fit the required input of an explicit constant.
Only way to deal with it will be to special-case it.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!