Non linear curve
3 views (last 30 days)
Show older comments
Dear all,
How can we plot nonlinear curve connecting set of data points in vector spaces.
4 Comments
Oleg Komarov
on 1 Jun 2011
Edit your post adding the code inside the cody and formatting it properly: http://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question
John
on 9 May 2024
Edited: John
on 9 May 2024
To plot a nonlinear curve connecting a set of data points in vector spaces , you can use the spline function along with plot. example:
% Data points
x = [-1, 0, 1, 2, 3];
y = [2, 1, 3, 2, 4];
% Generate a fine mesh of points for the curve
x_fine = linspace(min(x), max(x), 100);
% Interpolate using spline
y_fine = spline(x, y, x_fine);
% Plot the data points and the curve
plot(x, y, 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'blue');
hold on;
plot(x_fine, y_fine, 'r-', 'LineWidth', 2);
xlabel('x');
ylabel('y');
title('Nonlinear Curve Connecting Data Points');
legend('Data Points', 'Curve');
grid on;
- define the data points as vectors x and y.
- generate a fine mesh of points x_fine using linspace to create a smooth curve. The number of points can be adjusted as needed.
- use the spline function to interpolate the data points. It takes the original x and y data points and the fine mesh x_fine as inputs and returns the corresponding interpolated values y_fine.
- plot the data points using plot with circular markers ('o') in blue color.
- use hold on to keep the current plot and add the curve on top of it.
- plot the curve using plot with the fine mesh points x_fine and y_fine, specifying a red solid line ('-') with a line width of 2.
- add labels for the x-axis, y-axis, and title using xlabel, ylabel, and title, respectively.
- add a legend to identify the data points and the curve using legend.
- Finally, add a grid to the plot using grid on.
This code will produce a plot with the data points connected by a smooth nonlinear curve. The spline function is used for interpolation, which generates a piecewise polynomial curve that passes through all the data points.
Answers (0)
See Also
Categories
Find more on Smoothing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!