简体   繁体   中英

MATLAB Curve Fitting with Slopes

Background

I am currently working on a lecture for my Engineering in MATLAB course and stumbled upon a problem I would like to present to the class. I have made many different attempts to solve this problem, but my graphs keep coming out incorrect. I will describe the problem below and all the steps I took to try to solve this problem.

Problem

Find the coefficients of the fourth-degree polynomial

P(x) = ax^4 + bx^3 + cx^2 + dx + e

whose graph goes through the points (0, 1) , (1, 1) , (-1,3) , and whose slopes at x = -1 is 20 and at x = 1 is 9 .

Check your answer visually.

Attempt

I began by creating a matrix of the above x-values that I have derived as follows:

A = [0^4 0^3 0^2 0 1; 1^4 1^3 1^2 1 1; (-1)^4 (-1)^3 (-1)^2 -1 1];
A = [0 0 0 0 1; 1 1 1 1 1; 1 -1 1 -1 1];

This creates a 5 column by 3 row matrix that I may use to plot the polynomial.

My issue is that I am unable to get the last row of x-values, since each row is an equation in the system of equations and there must be as many equations as there are unknowns (4: a, b, c, and d are unknown, but e always equals 1 as you can see).

Ignoring this issue for a moment, I can continue to create a vertical matrix of y-values so that I may solve the system of equations. These y values are already given, so all I have to do is type this code in:

y = [1 1 3]';

Once again, there should be a fourth y-value to go along with the system of equations, but I have been unable to derive it using just the slopes of the points at x = -1 and x = 1 .

Once both the x-values and the y-values are derived, we can proceed to using the backslash operator (/) to solve the system of linear equations A*x = y.

p = A\y;

mldivide is more info on the mldivide function for anyone who needs reference.

From here on out, the following code which creates a polynomial from this system of equations and graphs it, should stay the same.

u = -1:.01:1;
v = polyval(p, u);
plot(u,v);

In this code, u is the domain of x-values from -1 to 1 with a 0.01 interval. This is needed by us to use the polyval function, which creates a polynomial from a system of equations we derived at p on the interval u .

Lastly, plot simply graphs our derived polynomial using MATLAB's GUI on the interval u .

As you can see, the only missing pieces I have are one more row of x-values in my matrix A and one y-value in matrix y that I need to find the four unknowns a, b, c, and d. I believe you must use the two slopes given in the problem to find each point. I have tried using the polyder function to get the derivative of the matrix p by doing,

q = polyder(p);

but I am still confused as to how to continue from there. Any help will be greatly appreciated.

I would calculate the derivative of the polynomial:

dP(x) = 4ax^3 + 3bx^2 + 2cx + d

Now, you know that dP(-1)=20 and dP(1)=9 so you have 5 equations with 5 unknowns:

e = 1
a + b + c + d + e = 1
a - b + c - d + e = 3
-4*a + 3*b - 2*c + d = 20
4*a + 3*b + 2*c + d = 9

So you can construct a 5x5 matrix and solve the system, as you did with A\\y .

The code to construct this 5x5 matrix is:

A = [0 0 0 0 1 ; 1 1 1 1 1 ; 1 -1 1 -1 1 ; -4 3 -2 1 0 ; 4 3 2 1 0];
y = [1 1 3 20 9]';

You can then check the results on a plot:

p=A\y;
u = -1:.01:1;
v = polyval(p, u);
data_pts = [0, 1; 1, 1;-1, 3]
plot(u,v,data_pts(:,1),data_pts(:,2),'rx')

which gives the following plot:

在此处输入图片说明

You can do the same with the derivative and checks it goes through the points (-1,20) and (1,9) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM