Non linear curve

3 views (last 30 days)
IEEE IE
IEEE IE on 1 Jun 2011
Edited: John on 9 May 2024
Dear all,
How can we plot nonlinear curve connecting set of data points in vector spaces.
  4 Comments
Oleg Komarov
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
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;
  1. define the data points as vectors x and y.
  2. generate a fine mesh of points x_fine using linspace to create a smooth curve. The number of points can be adjusted as needed.
  3. 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.
  4. plot the data points using plot with circular markers ('o') in blue color.
  5. use hold on to keep the current plot and add the curve on top of it.
  6. 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.
  7. add labels for the x-axis, y-axis, and title using xlabel, ylabel, and title, respectively.
  8. add a legend to identify the data points and the curve using legend.
  9. 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.

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!