ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 01 May 2020 14:17:23 +0200Error in polynomial interpolationhttps://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/I have edited this question to insist on what goes wrong
The following code ends with an indetermined parameter.
# Polynomial interpolation
# Passing the matrix
A = Matrix([[0,0],[0.5,0.25],[0.6,0.36],[2,4]])
var('a0, a1, a2, a3, a4')
B =[[A[i][1], a3*A[i][0]^3+ a2*A[i][0]^2+ a1*A[i][0]+ a0] for i in range(0,4)]
C=[[(B[i][0]-B[i][1])^2] for i in range(0,4)]
# sum of the square of errors
ssr=sum(C[i][0] for i in range(4))
# First order condition of optimality
ssr_a0=ssr.diff(a0)
ssr_a1=ssr.diff(a1)
ssr_a2=ssr.diff(a2)
ssr_a3=ssr.diff(a3)
ssr_a4=ssr.diff(a4)
# solution
sol=solve([ssr_a0==0,ssr_a1==0,ssr_a2==0,ssr_a3==0,ssr_a4==0], a0, a1, a2, a3, a4, solution_dict=True)
sol
The proposed solution being `[{a0: 0, a1: 0, a2: 1, a3: 0, a4: r3}]`.
But the `ssr_ai` $i = 0,\ldots, 3$ are all linear. So `sol` should gives the same result that the following code
C=matrix([[8, 6.2, 9.22, 16.682], [6.2, 9.22,16.68,32.3842],[9.22,16.682,32.3842,64.21802],[16.68,32.3842,64.21802,128.124562]])
b=vector([9.22,16.68,32.3842,64.21902])
C.solve_right(b)
So why it is not the case ? (There is one and only one solution)
This lead to a second question : for the `ssr_ai` how can I select the parameters to construct the `C` matrix and
the `b` vector not by hand as I have been obliged to do ?Thu, 30 Apr 2020 12:13:48 +0200https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/Comment by Sébastien for <p>I have edited this question to insist on what goes wrong</p>
<p>The following code ends with an indetermined parameter.</p>
<pre><code># Polynomial interpolation
# Passing the matrix
A = Matrix([[0,0],[0.5,0.25],[0.6,0.36],[2,4]])
var('a0, a1, a2, a3, a4')
B =[[A[i][1], a3*A[i][0]^3+ a2*A[i][0]^2+ a1*A[i][0]+ a0] for i in range(0,4)]
C=[[(B[i][0]-B[i][1])^2] for i in range(0,4)]
# sum of the square of errors
ssr=sum(C[i][0] for i in range(4))
# First order condition of optimality
ssr_a0=ssr.diff(a0)
ssr_a1=ssr.diff(a1)
ssr_a2=ssr.diff(a2)
ssr_a3=ssr.diff(a3)
ssr_a4=ssr.diff(a4)
# solution
sol=solve([ssr_a0==0,ssr_a1==0,ssr_a2==0,ssr_a3==0,ssr_a4==0], a0, a1, a2, a3, a4, solution_dict=True)
sol
</code></pre>
<p>The proposed solution being <code>[{a0: 0, a1: 0, a2: 1, a3: 0, a4: r3}]</code>. </p>
<p>But the <code>ssr_ai</code> $i = 0,\ldots, 3$ are all linear. So <code>sol</code> should gives the same result that the following code</p>
<pre><code>C=matrix([[8, 6.2, 9.22, 16.682], [6.2, 9.22,16.68,32.3842],[9.22,16.682,32.3842,64.21802],[16.68,32.3842,64.21802,128.124562]])
b=vector([9.22,16.68,32.3842,64.21902])
C.solve_right(b)
</code></pre>
<p>So why it is not the case ? (There is one and only one solution)</p>
<p>This lead to a second question : for the <code>ssr_ai</code> how can I select the parameters to construct the <code>C</code> matrix and
the <code>b</code> vector not by hand as I have been obliged to do ?</p>
https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51159#post-id-51159Please add what are your expectations. Deciphering the code allows to understand the expected result, but not what is in your mind.Thu, 30 Apr 2020 13:02:04 +0200https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51159#post-id-51159Comment by Cyrille for <p>I have edited this question to insist on what goes wrong</p>
<p>The following code ends with an indetermined parameter.</p>
<pre><code># Polynomial interpolation
# Passing the matrix
A = Matrix([[0,0],[0.5,0.25],[0.6,0.36],[2,4]])
var('a0, a1, a2, a3, a4')
B =[[A[i][1], a3*A[i][0]^3+ a2*A[i][0]^2+ a1*A[i][0]+ a0] for i in range(0,4)]
C=[[(B[i][0]-B[i][1])^2] for i in range(0,4)]
# sum of the square of errors
ssr=sum(C[i][0] for i in range(4))
# First order condition of optimality
ssr_a0=ssr.diff(a0)
ssr_a1=ssr.diff(a1)
ssr_a2=ssr.diff(a2)
ssr_a3=ssr.diff(a3)
ssr_a4=ssr.diff(a4)
# solution
sol=solve([ssr_a0==0,ssr_a1==0,ssr_a2==0,ssr_a3==0,ssr_a4==0], a0, a1, a2, a3, a4, solution_dict=True)
sol
</code></pre>
<p>The proposed solution being <code>[{a0: 0, a1: 0, a2: 1, a3: 0, a4: r3}]</code>. </p>
<p>But the <code>ssr_ai</code> $i = 0,\ldots, 3$ are all linear. So <code>sol</code> should gives the same result that the following code</p>
<pre><code>C=matrix([[8, 6.2, 9.22, 16.682], [6.2, 9.22,16.68,32.3842],[9.22,16.682,32.3842,64.21802],[16.68,32.3842,64.21802,128.124562]])
b=vector([9.22,16.68,32.3842,64.21902])
C.solve_right(b)
</code></pre>
<p>So why it is not the case ? (There is one and only one solution)</p>
<p>This lead to a second question : for the <code>ssr_ai</code> how can I select the parameters to construct the <code>C</code> matrix and
the <code>b</code> vector not by hand as I have been obliged to do ?</p>
https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51168#post-id-51168I should obtain a value for a0, a1, a3, and a4. So what is this result `a4 : r3` ? What means r3 ? I already encounter such a result but I have forgoten how to resolve it.Thu, 30 Apr 2020 17:21:47 +0200https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51168#post-id-51168Comment by Sébastien for <p>I have edited this question to insist on what goes wrong</p>
<p>The following code ends with an indetermined parameter.</p>
<pre><code># Polynomial interpolation
# Passing the matrix
A = Matrix([[0,0],[0.5,0.25],[0.6,0.36],[2,4]])
var('a0, a1, a2, a3, a4')
B =[[A[i][1], a3*A[i][0]^3+ a2*A[i][0]^2+ a1*A[i][0]+ a0] for i in range(0,4)]
C=[[(B[i][0]-B[i][1])^2] for i in range(0,4)]
# sum of the square of errors
ssr=sum(C[i][0] for i in range(4))
# First order condition of optimality
ssr_a0=ssr.diff(a0)
ssr_a1=ssr.diff(a1)
ssr_a2=ssr.diff(a2)
ssr_a3=ssr.diff(a3)
ssr_a4=ssr.diff(a4)
# solution
sol=solve([ssr_a0==0,ssr_a1==0,ssr_a2==0,ssr_a3==0,ssr_a4==0], a0, a1, a2, a3, a4, solution_dict=True)
sol
</code></pre>
<p>The proposed solution being <code>[{a0: 0, a1: 0, a2: 1, a3: 0, a4: r3}]</code>. </p>
<p>But the <code>ssr_ai</code> $i = 0,\ldots, 3$ are all linear. So <code>sol</code> should gives the same result that the following code</p>
<pre><code>C=matrix([[8, 6.2, 9.22, 16.682], [6.2, 9.22,16.68,32.3842],[9.22,16.682,32.3842,64.21802],[16.68,32.3842,64.21802,128.124562]])
b=vector([9.22,16.68,32.3842,64.21902])
C.solve_right(b)
</code></pre>
<p>So why it is not the case ? (There is one and only one solution)</p>
<p>This lead to a second question : for the <code>ssr_ai</code> how can I select the parameters to construct the <code>C</code> matrix and
the <code>b</code> vector not by hand as I have been obliged to do ?</p>
https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51170#post-id-51170`r3` is a parameter in the space of solution. In the documentation of `solve`, you can see this example: "*If there is a parameter in the answer, that will show up as a new variable. In the following example, "r1" is an arbitrary constant (because of the "r")*":
sage: forget()
sage: x, y = var('x,y')
sage: solve([x+y == 3, 2*x+2*y == 6],x,y)
[[x == -r1 + 3, y == r1]]Thu, 30 Apr 2020 18:03:52 +0200https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51170#post-id-51170Comment by Cyrille for <p>I have edited this question to insist on what goes wrong</p>
<p>The following code ends with an indetermined parameter.</p>
<pre><code># Polynomial interpolation
# Passing the matrix
A = Matrix([[0,0],[0.5,0.25],[0.6,0.36],[2,4]])
var('a0, a1, a2, a3, a4')
B =[[A[i][1], a3*A[i][0]^3+ a2*A[i][0]^2+ a1*A[i][0]+ a0] for i in range(0,4)]
C=[[(B[i][0]-B[i][1])^2] for i in range(0,4)]
# sum of the square of errors
ssr=sum(C[i][0] for i in range(4))
# First order condition of optimality
ssr_a0=ssr.diff(a0)
ssr_a1=ssr.diff(a1)
ssr_a2=ssr.diff(a2)
ssr_a3=ssr.diff(a3)
ssr_a4=ssr.diff(a4)
# solution
sol=solve([ssr_a0==0,ssr_a1==0,ssr_a2==0,ssr_a3==0,ssr_a4==0], a0, a1, a2, a3, a4, solution_dict=True)
sol
</code></pre>
<p>The proposed solution being <code>[{a0: 0, a1: 0, a2: 1, a3: 0, a4: r3}]</code>. </p>
<p>But the <code>ssr_ai</code> $i = 0,\ldots, 3$ are all linear. So <code>sol</code> should gives the same result that the following code</p>
<pre><code>C=matrix([[8, 6.2, 9.22, 16.682], [6.2, 9.22,16.68,32.3842],[9.22,16.682,32.3842,64.21802],[16.68,32.3842,64.21802,128.124562]])
b=vector([9.22,16.68,32.3842,64.21902])
C.solve_right(b)
</code></pre>
<p>So why it is not the case ? (There is one and only one solution)</p>
<p>This lead to a second question : for the <code>ssr_ai</code> how can I select the parameters to construct the <code>C</code> matrix and
the <code>b</code> vector not by hand as I have been obliged to do ?</p>
https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51177#post-id-51177Thanks Sebastien for your comment. But this is weird. My system is linear so either there is no solution either there is one. I have tried to substitute `find_root()` to `solve` but I suspect that it is only programmed to solve one variable equation. The solution would be to write the system in matrix notation but I do not know how to select the parameters to wtrite a matrix.Fri, 01 May 2020 07:00:28 +0200https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51177#post-id-51177Comment by Sébastien for <p>I have edited this question to insist on what goes wrong</p>
<p>The following code ends with an indetermined parameter.</p>
<pre><code># Polynomial interpolation
# Passing the matrix
A = Matrix([[0,0],[0.5,0.25],[0.6,0.36],[2,4]])
var('a0, a1, a2, a3, a4')
B =[[A[i][1], a3*A[i][0]^3+ a2*A[i][0]^2+ a1*A[i][0]+ a0] for i in range(0,4)]
C=[[(B[i][0]-B[i][1])^2] for i in range(0,4)]
# sum of the square of errors
ssr=sum(C[i][0] for i in range(4))
# First order condition of optimality
ssr_a0=ssr.diff(a0)
ssr_a1=ssr.diff(a1)
ssr_a2=ssr.diff(a2)
ssr_a3=ssr.diff(a3)
ssr_a4=ssr.diff(a4)
# solution
sol=solve([ssr_a0==0,ssr_a1==0,ssr_a2==0,ssr_a3==0,ssr_a4==0], a0, a1, a2, a3, a4, solution_dict=True)
sol
</code></pre>
<p>The proposed solution being <code>[{a0: 0, a1: 0, a2: 1, a3: 0, a4: r3}]</code>. </p>
<p>But the <code>ssr_ai</code> $i = 0,\ldots, 3$ are all linear. So <code>sol</code> should gives the same result that the following code</p>
<pre><code>C=matrix([[8, 6.2, 9.22, 16.682], [6.2, 9.22,16.68,32.3842],[9.22,16.682,32.3842,64.21802],[16.68,32.3842,64.21802,128.124562]])
b=vector([9.22,16.68,32.3842,64.21902])
C.solve_right(b)
</code></pre>
<p>So why it is not the case ? (There is one and only one solution)</p>
<p>This lead to a second question : for the <code>ssr_ai</code> how can I select the parameters to construct the <code>C</code> matrix and
the <code>b</code> vector not by hand as I have been obliged to do ?</p>
https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51178#post-id-51178"*But this is weird. My system is linear so either there is no solution either there is one.*" This is not true as a system of linear equations can have 0, 1 or an infinity of solutions. See the example in my previous comment.Fri, 01 May 2020 09:34:10 +0200https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51178#post-id-51178Comment by Cyrille for <p>I have edited this question to insist on what goes wrong</p>
<p>The following code ends with an indetermined parameter.</p>
<pre><code># Polynomial interpolation
# Passing the matrix
A = Matrix([[0,0],[0.5,0.25],[0.6,0.36],[2,4]])
var('a0, a1, a2, a3, a4')
B =[[A[i][1], a3*A[i][0]^3+ a2*A[i][0]^2+ a1*A[i][0]+ a0] for i in range(0,4)]
C=[[(B[i][0]-B[i][1])^2] for i in range(0,4)]
# sum of the square of errors
ssr=sum(C[i][0] for i in range(4))
# First order condition of optimality
ssr_a0=ssr.diff(a0)
ssr_a1=ssr.diff(a1)
ssr_a2=ssr.diff(a2)
ssr_a3=ssr.diff(a3)
ssr_a4=ssr.diff(a4)
# solution
sol=solve([ssr_a0==0,ssr_a1==0,ssr_a2==0,ssr_a3==0,ssr_a4==0], a0, a1, a2, a3, a4, solution_dict=True)
sol
</code></pre>
<p>The proposed solution being <code>[{a0: 0, a1: 0, a2: 1, a3: 0, a4: r3}]</code>. </p>
<p>But the <code>ssr_ai</code> $i = 0,\ldots, 3$ are all linear. So <code>sol</code> should gives the same result that the following code</p>
<pre><code>C=matrix([[8, 6.2, 9.22, 16.682], [6.2, 9.22,16.68,32.3842],[9.22,16.682,32.3842,64.21802],[16.68,32.3842,64.21802,128.124562]])
b=vector([9.22,16.68,32.3842,64.21902])
C.solve_right(b)
</code></pre>
<p>So why it is not the case ? (There is one and only one solution)</p>
<p>This lead to a second question : for the <code>ssr_ai</code> how can I select the parameters to construct the <code>C</code> matrix and
the <code>b</code> vector not by hand as I have been obliged to do ?</p>
https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51183#post-id-51183Sebastien Thanks for th answer. I am stupid. I was aware that I need a polynomial of degree $n-1$.Fri, 01 May 2020 14:17:23 +0200https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?comment=51183#post-id-51183Answer by Sébastien for <p>I have edited this question to insist on what goes wrong</p>
<p>The following code ends with an indetermined parameter.</p>
<pre><code># Polynomial interpolation
# Passing the matrix
A = Matrix([[0,0],[0.5,0.25],[0.6,0.36],[2,4]])
var('a0, a1, a2, a3, a4')
B =[[A[i][1], a3*A[i][0]^3+ a2*A[i][0]^2+ a1*A[i][0]+ a0] for i in range(0,4)]
C=[[(B[i][0]-B[i][1])^2] for i in range(0,4)]
# sum of the square of errors
ssr=sum(C[i][0] for i in range(4))
# First order condition of optimality
ssr_a0=ssr.diff(a0)
ssr_a1=ssr.diff(a1)
ssr_a2=ssr.diff(a2)
ssr_a3=ssr.diff(a3)
ssr_a4=ssr.diff(a4)
# solution
sol=solve([ssr_a0==0,ssr_a1==0,ssr_a2==0,ssr_a3==0,ssr_a4==0], a0, a1, a2, a3, a4, solution_dict=True)
sol
</code></pre>
<p>The proposed solution being <code>[{a0: 0, a1: 0, a2: 1, a3: 0, a4: r3}]</code>. </p>
<p>But the <code>ssr_ai</code> $i = 0,\ldots, 3$ are all linear. So <code>sol</code> should gives the same result that the following code</p>
<pre><code>C=matrix([[8, 6.2, 9.22, 16.682], [6.2, 9.22,16.68,32.3842],[9.22,16.682,32.3842,64.21802],[16.68,32.3842,64.21802,128.124562]])
b=vector([9.22,16.68,32.3842,64.21902])
C.solve_right(b)
</code></pre>
<p>So why it is not the case ? (There is one and only one solution)</p>
<p>This lead to a second question : for the <code>ssr_ai</code> how can I select the parameters to construct the <code>C</code> matrix and
the <code>b</code> vector not by hand as I have been obliged to do ?</p>
https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?answer=51179#post-id-51179If you look at the system of linear equations, you can see that it really has 4 equations with 4 unknowns (the 5th equation is 0=0 and the fifth unknown `a4` just doesn't appear)
sage: [ssr_a0==0,ssr_a1==0,ssr_a2==0,ssr_a3==0,ssr_a4==0]
[8*a0 + 6.20*a1 + 9.220000*a2 + 16.68200*a3 - 9.2200000 == 0,
6.20000*a0 + 9.2200000*a1 + 16.6820000*a2 + 32.3842000*a3 - 16.682000 == 0,
9.2200*a0 + 16.6820000*a1 + 32.384200*a2 + 64.21802000*a3 - 32.38420000 == 0,
16.68200*a0 + 32.384200*a1 + 64.2180200*a2 + 128.12456200*a3 - 64.2180200 == 0,
0 == 0]
That system has a solution involving only the first four unknown, which explains what you obtain:
[{a0: 0, a1: 0, a2: 1, a3: 0, a4: r3}]
Indeed, we can see that the coefficient of `a2` matches the constant terms. Alternatively, you can just remove `a4` in the call to solve since that variable does not appear in the system:
sage: solve([ssr_a0==0,ssr_a1==0,ssr_a2==0,ssr_a3==0,ssr_a4==0], a0, a1, a2, a3, solution_dict=True)
[{a0: 0, a1: 0, a2: 1, a3: 0}]
EDIT (answering a question in the comments): To transform the system of linear equations to matrices and solve the system, you may do:
sage: SSR = [ssr_a0,ssr_a1,ssr_a2,ssr_a3,ssr_a4]
sage: A = matrix([[ssr_a.coefficient(a) for a in [a0,a1,a2,a3]] for ssr_a in SSR])
sage: b = vector([-ssr_a.subs(a0=0,a1=0,a2=0,a3=0) for ssr_a in SSR])
sage: A \ b
(-4.81096644000283e-16, 8.08282642120849e-14, 0.999999999999817, 7.15728811560799e-14)
sage: A.change_ring(QQ) \ b.change_ring(QQ) # or in the ring of rationals
(0, 0, 1, 0)Fri, 01 May 2020 09:53:45 +0200https://ask.sagemath.org/question/51158/error-in-polynomial-interpolation/?answer=51179#post-id-51179