Matlab Plotting Points and a Cubic Polynomial that passes through them

In summary, In the given tutorial, the equations for a, b, c, and d were given. A function was defined to solve for these equations. However, the function was not used in the provided MATLAB code. The equations were written in a matrix form, and then an inverse matrix was found to solve for the coefficients.
  • #1
ver_mathstats
260
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
  • #2
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.
 
  • #3
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.
 

1. How do I plot a set of points on a graph using Matlab?

To plot a set of points on a graph using Matlab, you can use the "plot" function. This function takes in two vectors, one for the x-coordinates and one for the y-coordinates, and plots them as points on a graph. For example, if your x-coordinates are stored in a vector called "x" and your y-coordinates are stored in a vector called "y", you can use the command "plot(x,y)" to plot the points on a graph.

2. Can I plot a cubic polynomial that passes through a set of points using Matlab?

Yes, you can plot a cubic polynomial that passes through a set of points using Matlab. To do this, you can use the "polyfit" function to find the coefficients of the cubic polynomial that best fits the given points. Then, you can use the "polyval" function to evaluate the polynomial at a range of x-values and plot the resulting curve on the same graph as the points.

3. How do I customize the appearance of the plotted points and polynomial curve?

You can customize the appearance of the plotted points and polynomial curve by using additional arguments in the "plot" and "polyval" functions. For example, you can specify the color, marker type, and line style for the points and curve. You can also add a legend, axis labels, and a title to the graph using the "legend", "xlabel", "ylabel", and "title" functions.

4. Is it possible to plot multiple sets of points and polynomial curves on the same graph?

Yes, it is possible to plot multiple sets of points and polynomial curves on the same graph using Matlab. You can use the "hold on" command to hold the current plot and then use the "plot" and "polyval" functions to add additional points and curves to the same graph. Remember to use the "hold off" command when you are finished plotting to return to the default behavior of overwriting the current plot.

5. How can I use the plotted points and polynomial curve to make predictions for other values?

You can use the plotted points and polynomial curve to make predictions for other values by using the "polyval" function. This function takes in the coefficients of the polynomial and the x-values for which you want to make predictions, and returns the corresponding y-values. You can also use the "interp1" function to interpolate the curve at specific x-values, or the "polyfit" function to find the coefficients of a new polynomial that best fits the given points.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
814
  • Engineering and Comp Sci Homework Help
Replies
1
Views
887
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
829
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Introductory Physics Homework Help
Replies
3
Views
751
  • Engineering and Comp Sci Homework Help
Replies
1
Views
957
  • Engineering and Comp Sci Homework Help
Replies
6
Views
864
Back
Top