How to easily find the coefficients of a cubic polynomial and its plot for the given 4 points
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
Given 4 points as points = 0, 4, 1, 0, 2, -1, 3, 0;
. I want to find the coefficients of f[x_] := a x^3 + b x^2 + c x + d;
and its plot.
How to solve this in Mathematica easily?
plotting equation-solving interpolation
add a comment |Â
up vote
4
down vote
favorite
Given 4 points as points = 0, 4, 1, 0, 2, -1, 3, 0;
. I want to find the coefficients of f[x_] := a x^3 + b x^2 + c x + d;
and its plot.
How to solve this in Mathematica easily?
plotting equation-solving interpolation
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
Given 4 points as points = 0, 4, 1, 0, 2, -1, 3, 0;
. I want to find the coefficients of f[x_] := a x^3 + b x^2 + c x + d;
and its plot.
How to solve this in Mathematica easily?
plotting equation-solving interpolation
Given 4 points as points = 0, 4, 1, 0, 2, -1, 3, 0;
. I want to find the coefficients of f[x_] := a x^3 + b x^2 + c x + d;
and its plot.
How to solve this in Mathematica easily?
plotting equation-solving interpolation
plotting equation-solving interpolation
edited Aug 15 at 17:03
Carl Woll
57.3k273149
57.3k273149
asked Aug 15 at 7:43
Friendly Ghost
2076
2076
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
accepted
You can also use LinearSolve
A = CoefficientArrays[f@points[[All, 1]], a, b, c, d] //Last;
B = points[[All, 2]];
$A=left(
beginarraycccc
0 & 0 & 0 & 1 \
1 & 1 & 1 & 1 \
8 & 4 & 2 & 1 \
27 & 9 & 3 & 1 \
endarray
right) qquad B=left(
beginarrayc
4 \
0 \
-1 \
0 \
endarray
right)$
LinearSolve[A, B]
-(1/6), 2, -(35/6), 4
add a comment |Â
up vote
5
down vote
Four points define the polynomial unambiguously, not fitting is necesary, so for an exact solution I would do a system of equations and use Solve
points = 0, 4, 1, 0, 2, -1, 3, 0
(* 0, 4, 1, 0, 2, -1, 3, 0 *)
f[x_] := a x^3 + b x^2 + c x + d
(f[#1] == #2) & @@@ points
(*
d == 4,
a + b + c + d == 0,
8 a + 4 b + 2 c + d == -1,
27 a + 9 b + 3 c + d == 0
*)
Solve[
(f[#1] == #2) & @@@ points
, a, b, c, d
]
(* a -> -(1/6), b -> 2, c -> -(35/6), d -> 4 *)
add a comment |Â
up vote
5
down vote
fit = FindFit[points, f[x], a, b, c, d, x]
a -> -0.166667, b -> 2., c -> -5.83333, d -> 4.
Plot[Evaluate[f[x] /. fit], x, 0, 5,
Epilog -> PointSize[Large], Red, Point@points]
To get exact results, you can use
Rationalize[fit]
a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
Alternatively, you can use Reduce
or Solve
(as in rhermans's answer) with alternative specification of the first argument:
ToRules @ Reduce[f /@ points[[All, 1]] == points[[All, 2]]] (* or *)
Solve[f /@ points[[All, 1]] == points[[All, 2]], a, b, c, d][[1]]
a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
Could you make the coefficients in exact forms?
â Friendly Ghost
Aug 15 at 7:51
@FriendlyGhost, you can useRationalize[fit]
to geta -> -(1/6), b -> 2, c -> -(35/6), d -> 4
â kglr
Aug 15 at 7:54
@FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
â rhermans
Aug 15 at 10:12
add a comment |Â
up vote
2
down vote
You can use InterpolatingPolynomial
:
Expand @ InterpolatingPolynomial[
0,4,1,0,2,-1,3,0,
x
]
4 - (35 x)/6 + 2 x^2 - x^3/6
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
You can also use LinearSolve
A = CoefficientArrays[f@points[[All, 1]], a, b, c, d] //Last;
B = points[[All, 2]];
$A=left(
beginarraycccc
0 & 0 & 0 & 1 \
1 & 1 & 1 & 1 \
8 & 4 & 2 & 1 \
27 & 9 & 3 & 1 \
endarray
right) qquad B=left(
beginarrayc
4 \
0 \
-1 \
0 \
endarray
right)$
LinearSolve[A, B]
-(1/6), 2, -(35/6), 4
add a comment |Â
up vote
6
down vote
accepted
You can also use LinearSolve
A = CoefficientArrays[f@points[[All, 1]], a, b, c, d] //Last;
B = points[[All, 2]];
$A=left(
beginarraycccc
0 & 0 & 0 & 1 \
1 & 1 & 1 & 1 \
8 & 4 & 2 & 1 \
27 & 9 & 3 & 1 \
endarray
right) qquad B=left(
beginarrayc
4 \
0 \
-1 \
0 \
endarray
right)$
LinearSolve[A, B]
-(1/6), 2, -(35/6), 4
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
You can also use LinearSolve
A = CoefficientArrays[f@points[[All, 1]], a, b, c, d] //Last;
B = points[[All, 2]];
$A=left(
beginarraycccc
0 & 0 & 0 & 1 \
1 & 1 & 1 & 1 \
8 & 4 & 2 & 1 \
27 & 9 & 3 & 1 \
endarray
right) qquad B=left(
beginarrayc
4 \
0 \
-1 \
0 \
endarray
right)$
LinearSolve[A, B]
-(1/6), 2, -(35/6), 4
You can also use LinearSolve
A = CoefficientArrays[f@points[[All, 1]], a, b, c, d] //Last;
B = points[[All, 2]];
$A=left(
beginarraycccc
0 & 0 & 0 & 1 \
1 & 1 & 1 & 1 \
8 & 4 & 2 & 1 \
27 & 9 & 3 & 1 \
endarray
right) qquad B=left(
beginarrayc
4 \
0 \
-1 \
0 \
endarray
right)$
LinearSolve[A, B]
-(1/6), 2, -(35/6), 4
answered Aug 15 at 13:56
Okkes Dulgerci
3,0841615
3,0841615
add a comment |Â
add a comment |Â
up vote
5
down vote
Four points define the polynomial unambiguously, not fitting is necesary, so for an exact solution I would do a system of equations and use Solve
points = 0, 4, 1, 0, 2, -1, 3, 0
(* 0, 4, 1, 0, 2, -1, 3, 0 *)
f[x_] := a x^3 + b x^2 + c x + d
(f[#1] == #2) & @@@ points
(*
d == 4,
a + b + c + d == 0,
8 a + 4 b + 2 c + d == -1,
27 a + 9 b + 3 c + d == 0
*)
Solve[
(f[#1] == #2) & @@@ points
, a, b, c, d
]
(* a -> -(1/6), b -> 2, c -> -(35/6), d -> 4 *)
add a comment |Â
up vote
5
down vote
Four points define the polynomial unambiguously, not fitting is necesary, so for an exact solution I would do a system of equations and use Solve
points = 0, 4, 1, 0, 2, -1, 3, 0
(* 0, 4, 1, 0, 2, -1, 3, 0 *)
f[x_] := a x^3 + b x^2 + c x + d
(f[#1] == #2) & @@@ points
(*
d == 4,
a + b + c + d == 0,
8 a + 4 b + 2 c + d == -1,
27 a + 9 b + 3 c + d == 0
*)
Solve[
(f[#1] == #2) & @@@ points
, a, b, c, d
]
(* a -> -(1/6), b -> 2, c -> -(35/6), d -> 4 *)
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Four points define the polynomial unambiguously, not fitting is necesary, so for an exact solution I would do a system of equations and use Solve
points = 0, 4, 1, 0, 2, -1, 3, 0
(* 0, 4, 1, 0, 2, -1, 3, 0 *)
f[x_] := a x^3 + b x^2 + c x + d
(f[#1] == #2) & @@@ points
(*
d == 4,
a + b + c + d == 0,
8 a + 4 b + 2 c + d == -1,
27 a + 9 b + 3 c + d == 0
*)
Solve[
(f[#1] == #2) & @@@ points
, a, b, c, d
]
(* a -> -(1/6), b -> 2, c -> -(35/6), d -> 4 *)
Four points define the polynomial unambiguously, not fitting is necesary, so for an exact solution I would do a system of equations and use Solve
points = 0, 4, 1, 0, 2, -1, 3, 0
(* 0, 4, 1, 0, 2, -1, 3, 0 *)
f[x_] := a x^3 + b x^2 + c x + d
(f[#1] == #2) & @@@ points
(*
d == 4,
a + b + c + d == 0,
8 a + 4 b + 2 c + d == -1,
27 a + 9 b + 3 c + d == 0
*)
Solve[
(f[#1] == #2) & @@@ points
, a, b, c, d
]
(* a -> -(1/6), b -> 2, c -> -(35/6), d -> 4 *)
answered Aug 15 at 8:40
rhermans
21.6k439104
21.6k439104
add a comment |Â
add a comment |Â
up vote
5
down vote
fit = FindFit[points, f[x], a, b, c, d, x]
a -> -0.166667, b -> 2., c -> -5.83333, d -> 4.
Plot[Evaluate[f[x] /. fit], x, 0, 5,
Epilog -> PointSize[Large], Red, Point@points]
To get exact results, you can use
Rationalize[fit]
a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
Alternatively, you can use Reduce
or Solve
(as in rhermans's answer) with alternative specification of the first argument:
ToRules @ Reduce[f /@ points[[All, 1]] == points[[All, 2]]] (* or *)
Solve[f /@ points[[All, 1]] == points[[All, 2]], a, b, c, d][[1]]
a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
Could you make the coefficients in exact forms?
â Friendly Ghost
Aug 15 at 7:51
@FriendlyGhost, you can useRationalize[fit]
to geta -> -(1/6), b -> 2, c -> -(35/6), d -> 4
â kglr
Aug 15 at 7:54
@FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
â rhermans
Aug 15 at 10:12
add a comment |Â
up vote
5
down vote
fit = FindFit[points, f[x], a, b, c, d, x]
a -> -0.166667, b -> 2., c -> -5.83333, d -> 4.
Plot[Evaluate[f[x] /. fit], x, 0, 5,
Epilog -> PointSize[Large], Red, Point@points]
To get exact results, you can use
Rationalize[fit]
a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
Alternatively, you can use Reduce
or Solve
(as in rhermans's answer) with alternative specification of the first argument:
ToRules @ Reduce[f /@ points[[All, 1]] == points[[All, 2]]] (* or *)
Solve[f /@ points[[All, 1]] == points[[All, 2]], a, b, c, d][[1]]
a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
Could you make the coefficients in exact forms?
â Friendly Ghost
Aug 15 at 7:51
@FriendlyGhost, you can useRationalize[fit]
to geta -> -(1/6), b -> 2, c -> -(35/6), d -> 4
â kglr
Aug 15 at 7:54
@FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
â rhermans
Aug 15 at 10:12
add a comment |Â
up vote
5
down vote
up vote
5
down vote
fit = FindFit[points, f[x], a, b, c, d, x]
a -> -0.166667, b -> 2., c -> -5.83333, d -> 4.
Plot[Evaluate[f[x] /. fit], x, 0, 5,
Epilog -> PointSize[Large], Red, Point@points]
To get exact results, you can use
Rationalize[fit]
a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
Alternatively, you can use Reduce
or Solve
(as in rhermans's answer) with alternative specification of the first argument:
ToRules @ Reduce[f /@ points[[All, 1]] == points[[All, 2]]] (* or *)
Solve[f /@ points[[All, 1]] == points[[All, 2]], a, b, c, d][[1]]
a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
fit = FindFit[points, f[x], a, b, c, d, x]
a -> -0.166667, b -> 2., c -> -5.83333, d -> 4.
Plot[Evaluate[f[x] /. fit], x, 0, 5,
Epilog -> PointSize[Large], Red, Point@points]
To get exact results, you can use
Rationalize[fit]
a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
Alternatively, you can use Reduce
or Solve
(as in rhermans's answer) with alternative specification of the first argument:
ToRules @ Reduce[f /@ points[[All, 1]] == points[[All, 2]]] (* or *)
Solve[f /@ points[[All, 1]] == points[[All, 2]], a, b, c, d][[1]]
a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
edited Aug 15 at 13:02
answered Aug 15 at 7:49
kglr
160k8184384
160k8184384
Could you make the coefficients in exact forms?
â Friendly Ghost
Aug 15 at 7:51
@FriendlyGhost, you can useRationalize[fit]
to geta -> -(1/6), b -> 2, c -> -(35/6), d -> 4
â kglr
Aug 15 at 7:54
@FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
â rhermans
Aug 15 at 10:12
add a comment |Â
Could you make the coefficients in exact forms?
â Friendly Ghost
Aug 15 at 7:51
@FriendlyGhost, you can useRationalize[fit]
to geta -> -(1/6), b -> 2, c -> -(35/6), d -> 4
â kglr
Aug 15 at 7:54
@FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
â rhermans
Aug 15 at 10:12
Could you make the coefficients in exact forms?
â Friendly Ghost
Aug 15 at 7:51
Could you make the coefficients in exact forms?
â Friendly Ghost
Aug 15 at 7:51
@FriendlyGhost, you can use
Rationalize[fit]
to get a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
â kglr
Aug 15 at 7:54
@FriendlyGhost, you can use
Rationalize[fit]
to get a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
â kglr
Aug 15 at 7:54
@FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
â rhermans
Aug 15 at 10:12
@FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
â rhermans
Aug 15 at 10:12
add a comment |Â
up vote
2
down vote
You can use InterpolatingPolynomial
:
Expand @ InterpolatingPolynomial[
0,4,1,0,2,-1,3,0,
x
]
4 - (35 x)/6 + 2 x^2 - x^3/6
add a comment |Â
up vote
2
down vote
You can use InterpolatingPolynomial
:
Expand @ InterpolatingPolynomial[
0,4,1,0,2,-1,3,0,
x
]
4 - (35 x)/6 + 2 x^2 - x^3/6
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can use InterpolatingPolynomial
:
Expand @ InterpolatingPolynomial[
0,4,1,0,2,-1,3,0,
x
]
4 - (35 x)/6 + 2 x^2 - x^3/6
You can use InterpolatingPolynomial
:
Expand @ InterpolatingPolynomial[
0,4,1,0,2,-1,3,0,
x
]
4 - (35 x)/6 + 2 x^2 - x^3/6
answered Aug 15 at 17:01
Carl Woll
57.3k273149
57.3k273149
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180038%2fhow-to-easily-find-the-coefficients-of-a-cubic-polynomial-and-its-plot-for-the-g%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password