Matlab Plotting Points and a Cubic Polynomial that passes through them

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 3K views
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.