Help with quadriatic formula?

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 个评论

To clarify, your input is something like this following, correct?
equation = '3x^2 + 5x + 12=0'
There are lots of parsers for quadratic equations based on regular expressions. This might be easier than using strtok
Oh, indeed! The OPs instructor is, however, apparently not amenable to alternate solutions than those suggested based on his comments later on.

请先登录,再进行评论。

 采纳的回答

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 个评论

Thanks for the help. Now, I am able to extract the first number usinf the strtok, but how do I get the the other 2 numbers?
How carefully are you controlling the format of the input the user provides? If the structure of the equation can be assumed to match a given format it's possible to simply do something like
>> s='3x^2+5x+1';
>> c=sscanf(s,'%f x^2 + %f x + %f')'
c =
3 5 1
>>
If you need to or want to use strtok() to parse substrings, simply use the optional "remainder" output and a while loop to return subsequent tokens until it becomes the empty string.
[t,r]=strtok(s);
% process token appropriately here
while ~isempty(r)
[t,r]=strtok(s);
% process the next token appropriately here
end
MAB
MAB 2013-9-16
编辑:MAB 2013-9-17
I was able to talk to the instructor today and he gave a little more clarification for the problem.
he says the user is going to enter the string as follows as a string:
'4x^2+2x+3=0'
Then we are to use strtok() to pull the values for A B & C.
ie A = strtok(s , 'x') will pull out the 4 from the string. The above strtok example is the only way he has showed us and he normally doesn't want us to deviate far from that, but I do not see how using it in that format I can pull out B and C? Im sure I could use the [t,r]=strtok(s,...) but he says we are not allowed to use conditional statements like while and for loops.
That's just evil imo... :)
I don't see any good way to actually parse any of the other numeric values after the first with strtok() only--it just doesn't parse regularly based on the rules strtok() uses.
You could do a two-step process of first separating the terms based on defining the delimiter string as '+=' as the first pass then delimiter "x" on that token for a and b. But c is still an oddball that then parses on the first pass owing to using += for the primary delimiter.
Sotoo,
delim='+=';
[a,r]=strtok(s,delim); a=strtok(a,'x');
[b,r]=strtok(r,delim); b=strtok(b,'x');
c=strtok(r,delim);
Of course, all are still strings, not numeric; you still need a str2num() around each or sscanf() or somesuch to get the actual numeric value.
All in all, I'm not impressed by the instructions received...
Below is the code I got but like you said I still have to use str2num() to get actually values for my ABC.
% The user input the formula they want solved
function [xpos, xneg] = Roots(s)
%Quadratic equation format is = Ax^2 + Bx + C = 0
M = strtok(s,'x');
[a,b] = strtok(s, '+-');
T = strtok(b,'x')
[c,d]=strtok(b, '+-');
H = strtok(d, '=');
A = str2num(M)
B = str2num(T)
C = str2num(H)
%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
This code works by the way. The str2num is what I was needing to change my string values to numeric values.
Well, it'll be interesting to hear if the instructor actually can come up with a realistic parser under the given rules himself...keep us posted.
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.
The form doesn't fit the required input of an explicit constant.
Only way to deal with it will be to special-case it.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Historical Contests 的更多信息

提问:

MAB
2013-9-12

评论:

dpb
2013-10-17

Community Treasure Hunt

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

Start Hunting!

Translated by