Matlab Plotting Points and a Cubic Polynomial that passes through them

Click For Summary
SUMMARY

The discussion focuses on solving for the coefficients of a cubic polynomial \(y = ax^3 + bx^2 + cx + d\) that passes through the points (-1, 3), (0, 8), (1, 1), and (2, 5) using MATLAB. The user initially attempts to create a matrix \(A\) for a quadratic function but is guided to modify it to accommodate a cubic function by including \(xi.^3\) and ensuring the matrix dimensions are correct. The final approach involves setting up a system of equations and using MATLAB to solve for the coefficients by forming a 4x4 matrix equation.

PREREQUISITES
  • Understanding of cubic polynomials and their general form.
  • Familiarity with MATLAB syntax and matrix operations.
  • Knowledge of how to use the ones() function in MATLAB.
  • Ability to interpret and manipulate matrix equations.
NEXT STEPS
  • Learn how to set up and solve systems of linear equations in MATLAB.
  • Explore MATLAB's matrix manipulation functions, particularly the inv() function for matrix inversion.
  • Study the use of function handles in MATLAB for defining polynomial functions.
  • Investigate plotting techniques in MATLAB to visualize polynomial functions alongside data points.
USEFUL FOR

Students and educators in mathematics or engineering fields, MATLAB users working on polynomial fitting, and anyone interested in numerical methods for solving equations.

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
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · 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