MATLAB Answers

From symbolic to numerical results for quadratic equation

1 view (last 30 days)
laura bagnale
laura bagnale on 17 Jun 2021
Edited: Walter Roberson on 19 Jun 2021
Hello everyone,
I hope someone could help me, I'm starting using the symbolic toolbox but I have some difficulties.
I wrote this quadratic equation in symbolic form:
syms x y a1 b1 c1 d1 e1 f1
a = d1*y^2 + (e1 + b1*x)*y + a1*x^2 + c1*x + f1
then I tried to solve it:
ht=matlabFunction(a)
eqn = d1.*y.^2+(e1+b1.*x).*y+(a1.*x.^2+c1.*x+f1) == 0
S = solve(eqn, y)
I got what I expected from the formula
[-(e1 + b1*x - sqrt(b1^2*x^2 + 2*b1*e1*x + e1^2 - 4*a1*d1*x^2 - 4*c1*d1*x - 4*d1*f1))/(2*d1); -(e1 + b1*x + sqrt(b1^2*x^2 + 2*b1*e1*x + e1^2 - 4*a1*d1*x^2 - 4*c1*d1*x - 4*d1*f1))/(2*d1)]
However I don't know how to get the numerical real solutions.
I know the numerical values for the coeffcients a1, b1, c1, d1, e1, f1.
I tried to write the equation in this way:
syms x y
a = d1*y^2 + (e1 + b1*x)*y + a1*x^2 + c1*x + f1
ht=matlabFunction(a)
eqn = d1.*y.^2+(e1+b1.*x).*y+(a1.*x.^2+c1.*x+f1) == 0
S = solve(eqn, y, 'Real', true)
but the formula appears in the same form, with numbers in the places of the coefficients but with no solutions.
Can someone help me please.
Thank you very much in advance.
Laura
  2 Comments
laura bagnale
laura bagnale on 17 Jun 2021
Thank you a lot KSSV for your quick reply! I will try with subs as you suggested.
Yes, I defined the coefficients.
This is the part of the code regarding my question, the previous one is very long. I obtained pcell and qcell by using poly22 to interpolate 5 points with a surface in space.
[p00, p10, p01, p20, p11, p02] = pcell{:}
[q00, q10, q01, q20, q11, q02] = qcell{:}
format short
a1 = p20.*q11.*2.0-p11.*q20.*2.0
b1 = p20.*q20.*4.0-p02.*q20.*4.0
c1 = p10.*q11-p01.*q20.*2.0-p11.*q10+p20.*q01.*2.0
d1 = p11.*q20.*2.0-p02.*q11.*2.0
e1 = p11.*q01-p02.*q10.*2.0-p01.*q11+p10.*q20.*2.0
f1 = p10.*q01-p01.*q10
a1 = -0.0870
b1 = -0.2519
c1 = -0.0847
d1 = -0.0495
e1 = -0.0386
f1 = -0.0081
g = @(x,y) a1.*x.^2 + b1.*x.*y + c1.*x + d1.*y.^2 + e1.*y + f1
syms x y a1 b1 c1 d1 e1 f1
eqn = a1.*x.^2 + b1.*x.*y + c1.*x + d1.*y.^2 + e1.*y + f1
a = collect(eqn, y)
ht=matlabFunction(a)
eqn = d1.*y.^2+(e1+b1.*x).*y+(a1.*x.^2+c1.*x+f1) == 0
S = solve(eqn, y)
syms x y a1 b1 c1 d1 e1 f1
eqn = a1.*x.^2 + b1.*x.*y + c1.*x + d1.*y.^2 + e1.*y + f1
a = collect(eqn, y)
ht=matlabFunction(a)
eqn = d1.*y.^2+(e1+b1.*x).*y+(a1.*x.^2+c1.*x+f1) == 0
S = solve(eqn, y)
Thank you very much for your help and support.
Laura

Sign in to comment.

Accepted Answer

Stephan
Stephan on 17 Jun 2021
Edited: Stephan on 17 Jun 2021
You could use symbolic functions:
syms y(a,b,x)
y(a,b,x) = a*x^2 - b*x
y(a, b, x) = 
% Calculate values for a=1, b=2 and x in a range from 1:4 with stepwide 0.5
result_symbolic = y(1,2,1:0.5:4)
result_symbolic = 
if you want that numbers (still symbolic) use double
result_numeric = double(result_symbolic)
result_numeric = 1×7
-1.0000 -0.7500 0 1.2500 3.0000 5.2500 8.0000
  2 Comments
laura bagnale
laura bagnale on 17 Jun 2021
I mean my difficulty is that the function is quadratic with 2 variables. I didn't find something similar from the MathWorks.
Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!