6
$\begingroup$

Let $f:[0,2]\to\Bbb{R}^+$ be the function give by $$f(x)=\sqrt{x(2-x)}.$$
I need to find (the exact values of) constants $a_1, a_2,a_3,a_4$ in $$g(x)=\sum_{k=1}^4a_kx^k(2-x)^k$$ such that $f(x)=g(x)$ for all $x\in\{ \frac14,\frac12, \frac34, 1\}.$

Is there any easy way to do this using mathematica?

Also I would like to compare the graphs of $f$ and $g$.

Since I am not familiar with mathematica, can somebody help me to solve this. Any help would be appreciate.

$\endgroup$
3
  • 1
    $\begingroup$ Removing the endpoints helps but the equations are still linearly dependent because the equation $f(1/4)=g(1/4)$ is identical to the equation $f(7/4)=g(7/4)$ and so on. $\endgroup$
    – user484
    Dec 26, 2016 at 20:41
  • 1
    $\begingroup$ @Nil Same technique works for your new domain of $k$. In fact it gives the same answer for the four a[n] variables as it should. $\endgroup$
    – Edmund
    Dec 26, 2016 at 20:58
  • $\begingroup$ @Edmund: Thank you very much for your helpful detailed solution. $\endgroup$
    – Bumblebee
    Dec 26, 2016 at 21:00

2 Answers 2

10
$\begingroup$

You may use LinearSolve to solve for the a[n].

Let

f[x_] := Sqrt[x (2 - x)]
g[x_] = Sum[a[k]*x^k*(2 - x)^k, {k, 0, 8}]

Note that I intentionally used Set instead of SetDelayed for g.

With

vars = a[#] & /@ Range[0, 8];
points = Range[0, 2, 1/4];

Then

m = CoefficientArrays[g[#], vars] & /@ points // Normal // #[[All, 2]] & ;
b = f /@ points ;

Call LinearSolve

ls = LinearSolve[m, b] // Simplify;

and construct Rules of solution (ls) and variables (vars).

sol = MapThread[Rule, {vars, ls}];
sol // Column

Mathematica graphics

Check equality for points by ReplaceAll of g with Rules in solution sol.

f[#] == g[#] /. sol & /@ points
{True, True, True, True, True, True, True, True, True}

Plot comparison.

Plot[{f[x], g[x] /. sol}, {x, 0, 2}, PlotLegends -> "Expressions", ImageSize -> Large]

Mathematica graphics

Hope this helps

$\endgroup$
4
  • 1
    $\begingroup$ Interesting, I didn't realize LinearSolve doesn't give a warning when the system is underdetermined. $\endgroup$
    – user484
    Dec 26, 2016 at 20:45
  • $\begingroup$ @Rahul Yes. It just comes out with the many zeros from the row reduction. $\endgroup$
    – Edmund
    Dec 26, 2016 at 20:48
  • 4
    $\begingroup$ vars = Array[a, 9, 0] $\endgroup$
    – corey979
    Dec 26, 2016 at 20:49
  • $\begingroup$ @corey979 That is nice. (+1) I don't use indexed variables that often so Array didn't come to mind. $\endgroup$
    – Edmund
    Dec 26, 2016 at 20:51
7
$\begingroup$

With the last update that reads:

Let $f:[0,2]\to\Bbb{R}^+$ be the function give by $$f(x)=\sqrt{x(2-x)}.$$
I need to find (the exact values of) constants $a_1, a_2,a_3,a_4$ in $$g(x)=\sum_{k=1}^4a_kx^k(2-x)^k$$ such that $f(x)=g(x)$ for all $x\in\{ \frac14,\frac12, \frac34, 1\}.$


Let's

f[x_] := Sqrt[x (2 - x)];
g[x_] := Sum[a[k] x^k (2 - x)^k, {k, 1, 4}];
vals = Range[1/4, 1, 1/4];
sol = Solve[g[#] == f[#] & /@ vals, Array[a, 4]][[1]];

Column @ sol

enter image description here

Matrix of the coefficients of the system of equations:

(m = Normal @
CoefficientArrays[g[#] == f[#] & /@ vals, 
  Array[a, 4]][[2]]) // MatrixForm

enter image description here

MatrixRank @ m

4

so all equations are linearly independent.

g2[x_] = g[x] /. sol // Simplify

enter image description here

and

Plot[{f[x], g2[x]}, {x, 0, 2}, PlotLegends -> "Expressions"]

enter image description here


Alternatively, one can actually fit a function to a dataset (below is a different case than previously):

Clear[f, g, vals]
f[x_] := Sqrt[x (2 - x)]
g[x_] := Sum[a[k] x^k (2 - x)^k, {k, 1, 8}]
args = Range[0, 2, 1/4];
vals = f /@ args;
data = Transpose @ {args, vals};

nlm = NonlinearModelFit[data, g[x], Array[a, 8], x];

nlm["ParameterTable"]

enter image description here

I skipped the a[0] to make the number of parameters smaller than the number of points to be fitted to.

g3[x_] = Normal @ nlm

enter image description here

Plot[{f[x], g3[x]}, {x, 0, 2}, PlotLegends -> "Expressions"]

enter image description here

Comparing the difference between g2 and g3:

Plot[g2[x] - g3[x], {x, 0, 2}]

enter image description here

(Beware of the Runge's phenomenon (1)(2)!)

$\endgroup$
3
  • $\begingroup$ Than you very much. I appreciate your continuous support to formulate the right question (+1). $\endgroup$
    – Bumblebee
    Dec 26, 2016 at 21:06
  • $\begingroup$ Just to add a note: one can pretty much expect the Runge phenomenon to happen for sufficiently high degree if your interpolation points are not "clustered" near the endpoints. $\endgroup$ Jan 3, 2017 at 13:52
  • $\begingroup$ Or simply Solve@ Table[Sum[a[k] (x (2 - x))^k, {k, 4}] == f[x], {x, Range[4]/4}]... $\endgroup$
    – Michael E2
    Jan 4, 2017 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.