Matlab Plotting Points and a Cubic Polynomial that passes through them

Click For Summary
To find the coefficients a, b, c, and d for the cubic polynomial y = ax^3 + bx^2 + cx + d that passes through the points (-1, 3), (0, 8), (1, 1), and (2, 5), you need to set up a system of equations based on these points. The matrix A should be constructed as a 4x4 matrix, including terms for x^3, x^2, x, and a constant term, with the corresponding y-values as a vector b. The code provided in the tutorial needs to be modified to reflect this cubic structure, ensuring the dimensions match. After setting up the equations, MATLAB can be used to solve for the coefficients by finding the inverse of the matrix A. This process will allow you to plot the cubic polynomial and verify that it passes through all specified points.
ver_mathstats
Messages
258
Reaction score
21

Homework Statement


We were given a tutorial to complete which I did complete.

Now the question is:

By modifying the appropriate lines in your script file, find the values of a, b, c, and d so that the cubic polynomial  y = ax3 + bx2 + cx + d  passes through the (x, y) pairs (-1, 3), (0, 8), (1, 1), and (2, 5).

Note that to check your answer you can plot the given points together with your cubic polynomial on the same graph, and check to see that all 4 points lie on the curve (as in the tutorial file). Note that you will likely have to modify the t vector so that it corresponds with the range of x-values above.

Homework Equations

The Attempt at a Solution


So far I have this

xi=[-1:2]'
yi=[3 8 1 5]'
A=[xi.^2 xi ones(3,1)] (this is from the tutorial I did, I am not sure how to change it so that it is a cubic function rather than a parabola. Would it be A=[xi.^3 xi.^2 xi. ones(2,-1)]?
b=yi

So now I would have

xi=[-1:2]'
yi=[3 8 1 5]'
A=[xi.^3 xi.^2 xi. ones(2,-1)]
b=yi

I am unsure of what to do next however?

In the tutorial I was given this

f = @(t) x(1)*t.^2+x(2)*t+x(3);
t = 0.5:.01:3.5;
y = f(t);

But I am unsure of how to modify this for my own question.

Thank you.
 
Physics news on Phys.org
ver_mathstats said:
In the tutorial I was given this

f = @(t) x(1)*t.^2+x(2)*t+x(3);
t = 0.5:.01:3.5;
y = f(t);
The first line in your code above is a function definition. I am not sure whether you are supposed to use it in you MATLAB code.

In a nutshell, you need MATLAB to solve the system of four equations in the unknowns a, b, c, and d:
a(-1)^3 + b(-1)^2 + c(-1) + d = 3
a(0)^3 + b(0)^2 + c(0) + d = 8
a(1)^3 + b(1)^2 + c(1) + d = 1
a(2)^3 + b(2)^2 + c(2) + d = 5

One approach would be to write the system as a matrix equation, and then find the inverse of the matrix, thereby solving for the vector of coefficients [a b c d]^T.
 
The ones() function in Matlab creates an array (matrix) of a certain size. Take a look at the documentation. https://www.mathworks.com/help/distcomp/ones.html
So ones(3,1) creates a "column vector" size 3 x 1. For a cubic, you now have 4 unknowns. Your A matrix should be 4x4 and your ones column needs to be 4x1, to match the other columns.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
2
Views
2K