Reputation: 259
I want to plot the function "y = 4.8643*x - 3.8559*x^2 + 1.1208*x^3 - 0.1348*x^4 + 0.0057*x^5" within x = 0 and x = 9. There should be 1000 intermediate values to make the function look smooth. I also want to plot the points from the function on the positions x = [0, 0.5, 1, 6, 7, 9] in the same window. How to do this?
Thanks!
Upvotes: 0
Views: 373
Reputation: 5190
You can use the function linspace to generate the x
values.
Then you can plot the function using the plot function
You also have to modify the way you define the function by adding a .
(a dot)
before the ^
operator in order to raise to the desired power the x
values
elementwise
To also plot the specific x
points, you can evaluate the y
function on that points and then either specify then in the
same call to plot
or to call again it after having set hold on
to add the new data
% Generate the `x` values
x=linspace(0,9,1000)
% Evaluate the `y` function in the `[0 9]` interval
y = 4.8643*x - 3.8559*x.^2 + 1.1208*x.^3 - 0.1348*x.^4 + 0.0057*x.^5
% Define the set of `x` data
x1 = [0, 0.5, 1, 6, 7, 9]
% Evaluate the `y` function in the new `x` interval
y1=4.8643*x1 - 3.8559*x1.^2 + 1.1208*x1.^3 - 0.1348*x1.^4 + 0.0057*x1.^5
Using a single call to plot
plot(x,y,x1,y1,'*')
grid on
Using two calls to plot
plot(x,y)
hold on
plot(x1,y1,'*')
Upvotes: 1