system of equations calculator

505 Views Asked by At

I have a problem that is very simple in concept but very difficult to actually solve.

I need to find the best fit polynomial of degree at most 4 for f(x)=ln(x) on the interval [1,4].

So far I have gotten to the point where I need to solve the system of equations: $0=-3a_0-\frac{15a_1}{2}-21a_2-\frac{255a_3}{4}-\frac{1023a_4}{5}-3+ln\left(256\right),\\0=-\frac{15a_0}{2}-21a_1-\frac{255a_2}{4}-\frac{1023a_3}{5}-\frac{1365a_4}{2}-\frac{15}{4}+8ln\left(4\right),\\0=-21a_0-\frac{255a_1}{4}-\frac{1023a_2}{5}-\frac{1365a_3}{2}-\frac{16383a_4}{7}-7+\frac{64}{9}ln\left(64\right),\\0=-\frac{255a_0}{4}-\frac{1023a_1}{5}-\frac{1365a_2}{2}-\frac{16383a_3}{7}-\frac{65535a_4}{8}-\frac{255}{16}+64ln\left(4\right),\\0=-\frac{1023a_0}{5}-\frac{1365a_1}{2}-\frac{16383a_2}{7}-\frac{65535a_3}{8}-29127a_4-\frac{1023}{25}+\frac{1024ln\left(4\right)}{5}$

I know how to do this in theory, but the execution is incredibly tedious and errors easily find their way in. I was wondering if anyone knew a good system of equation calculator, symbolab seems to only be able to solve up to 4 terms, or code for solving a 5 term system of equations on matlab.

2

There are 2 best solutions below

0
On

There are a number of ways to do it. You must specify exactly what you mean by 'the best fit polynomial', but to solve the system of equations you provided you can simply hard code it:

A = [-3 -15/2 -21 -255/4 -1023/5
    -15/2 -21 -255/4 -1023/5 -1365/2
    -21 -255/4 -1023/5 -1365/2 -16383/7
    -255/4 -1023/5 -1365/2 -16383/7 -65535/8
    -1023/5 -1365/2 -16383/7 -65535/8 -29127];
b = [3-log(256)
    15/4-8*log(4)
    7-64/9*log(64)
    255/16-64*log(4)
    1023/25-1024*log(4)/8];

a = A\b

a =

   1.0e+04 *

   -0.6548
    1.2611
   -0.8486
    0.2386
   -0.0239

I don't know exactly what the $a_i$ terms mean in your equation, but this is how you get them. You should be more clear on how you derived this system of equations, so the code calculates the coefficient matrix by itself, without entering the matrix by hand.

I warn you, however, there are other ways to obtain a curve fitting in MATLAB.

Polynomial fit:

If your polynomial has the form

$ p(x)=a_4x^4+a_3x^3+a_2x^2+a_1x+a_0 $

then you have 5 unkowns. Provided you have your function evaluated in 5 points, you can solve for the 5 unkowns rearrangin the above equation in a matrix form:

$ \begin{bmatrix} x_1^4 & x_1^3 & x_1^2 & x_1 & 1 \\ x_2^4 & x_2^3 & x_2^2 & x_2 & 1 \\ x_3^4 & x_3^3 & x_3^2 & x_3 & 1 \\ x_4^4 & x_4^3 & x_4^2 & x_4 & 1 \\ x_5^4 & x_5^3 & x_5^2 & x_5 & 1 \\ \end{bmatrix} \begin{Bmatrix} a_4\\a_3\\a_2\\a_1\\a_0 \end{Bmatrix}= \begin{Bmatrix} \log(x_1)\\\log(x_2)\\\log(x_3)\\\log(x_4)\\\log(x_5) \end{Bmatrix} $

Perhaps this was what you tried to do? You can automatically code it as:

x = linspace(1,4,5)';
fx = log(x);

A = [x.^4 x.^3 x.^2 x ones(size(x))]
b = fx
p = A\b

p =

   -0.0091
    0.1202
   -0.6393
    1.9070
   -1.3789

This returns the coefficients of the polynomial as defined above. You can do it automatically, however, using a MATLAB proper function.

The function polyval does a polynomial curve fitting in a least-squares sense. If you want the exact interpolation for a 4th order polynomial, then you should provide it with 5 points:

x = linspace(1,4,5)
fx = log(x)
p=polyfit(x,fx,4);
xq = 1:0.1:4;
plot(x,fx,'o',xq,polyval(p,xq),':');
legend('ln(x)','interpolated polynomial','location','best')

enter image description here

The array p has the polynomial coefficients of the curve fit. You can evaluate the polynomial with the polyval function. Notice this generates the same polynomial coefficients as calculated above with the backlash operator.

If you provide more points than the necessary, MATLAB's polyfit function will find the best polynomial in a least squares sense.

x = 1:0.1:4;
fx = log(x);
p=polyfit(x,fx,4);
xq = 1:0.01:4;
plot(x,fx,'o',xq,polyval(p,xq),':');
legend('ln(x)','interpolated polynomial','location','best')

enter image description here

The nodes don't necessarily must be evenly spaced. As a matter of fact, you can use the Chebyshev nodes to minimize the effect of Runge's phenomenon.

% Chebyshev nodes with exactly 5 points
n = 4;
k = 0:n;
a = 1;
b=4;
xk = (a+b)/2 - (b-a)/2*cos(k/n*pi)
fx = log(xk)
p=polyfit(xk,fx,4);
xq = 1:0.01:4;
plot(xk,fx,'o',xq,polyval(p,xq),':');
legend('ln(x)',['interpolated polynomial',newline,'Chebyshev nodes'],'location','best')

enter image description here

0
On

Problem : Fitting to $\ln(x)$ a 4-th degree polynomial $\quad a_0+a_1x+a_2x^2+a_3 x^3+a_4 x^4\quad $ on the range $1\leq x\leq 4$.

The sum of squared errors is : $$\epsilon^2=\int_1^{4}\left(a_0+a_1x+a_2x^2+a_3x^3+a_4x^4-\ln(x) \right)^2dx$$ The minimum is obtained when the partial derivatives (wrt the coefficients) are all null. $$\frac{\partial\epsilon^2}{\partial a_k}=2\int_1^{4}\left(a_0+a_1x+a_2x^2+a_3x^3+a_4x^4-\ln(x) \right)x^k dx =0 $$ This is for $k=0$ to $k=4$.

So we have five linear equations to be solved for the five unknowns $a_0,a_1,a_2,a_3,a_4$.

$$a_0\int_1^{4}x^{k} dx+a_1\int_1^{4}x^{k+1} dx+a_2\int_1^{4}x^{k+2} dx+a_3\int_1^{4}x^{k+3} dx+a_4\int_1^{4}x^{k+4} dx =$$ $$=\int_1^{4}\ln(x)\,x^k dx $$

After computing the integrals and writting the system of equations on matrix form :

enter image description here

On the range $\quad 0\leq x\leq 4\quad$ the Root Least Mean Square Error is $=0.001005$

On a graphical representation it is quite impossible to see a difference between the curves of the logarithm and the polynomial.

Following the integral method shown above it is easy to solve similar problems with higher polynomials and on different ranges of $x$.

For example with a polynomial of degree 5 :

enter image description here