Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve function approximation task in Python?

Consider the complex mathematical function on the line [1, 15]: f(x) = sin(x / 5) * exp(x / 10) + 5 * exp(-x / 2)

enter image description here

polynomial of degree n (w_0 + w_1 x + w_2 x^2 + ... + w_n x^n) is uniquely defined by any n + 1 different points through which it passes. This means that its coefficients w_0, ... w_n can be determined from the following system of linear equations:

enter image description here

Where x_1, ..., x_n, x_ {n + 1} are the points through which the polynomial passes, and by f (x_1), ..., f (x_n), f (x_ {n + 1}) - values that it must take at these points.

I'm trying to form a system of linear equations (that is, specify the coefficient matrix A and the free vector b) for the polynomial of the third degree, which must coincide with the function f at points 1, 4, 10, and 15. Solve this system using the scipy.linalg.solve function.

A = numpy.array([[1., 1., 1., 1.], [1., 4., 8., 64.], [1., 10., 100., 1000.], [1., 15., 225., 3375.]])

V = numpy.array([3.25, 1.74, 2.50, 0.63])

numpy.linalg.solve(A, V)

I got the wrong answer, which isenter image description here

So the question is: is the matrix correct?

like image 601
plywoods Avatar asked Dec 15 '25 04:12

plywoods


1 Answers

No, your matrix is not correct.

The biggest mistake is your second sub-matrix for A. The third entry should be 4**2 which is 16 but you have 8. Less important, you have only two decimal places for your constants array V but you really should have more precision than that. Systems of linear equations are sometimes very sensitive to the provided values, so make them as precise as possible. Also, the rounding in your final three entries is bad: you rounded down but you should have rounded up. If you really want two decimal places (which I do not recommend) the values should be

V = numpy.array([3.25, 1.75, 2.51, 0.64])

But better would be

V = numpy.array([3.252216865271419, 1.7468459495903677,
                 2.5054164070002463, 0.6352214195786656])

With those changes to A and V I get the result

array([ 4.36264154, -1.29552587,  0.19333685, -0.00823565])

I get these two sympy plots, the first showing your original function and the second using the approximated cubic polynomial.

enter image description here

enter image description here

They look close to me! When I calculate the function values at 1, 4, 10, and 15, the largest absolute error is for 15, namely -4.57042132584462e-6. That is somewhat larger than I would have expected but probably is good enough.

like image 71
Rory Daulton Avatar answered Dec 16 '25 21:12

Rory Daulton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!