How can plot with smooth line?

조회 수: 366 (최근 30일)
Riyadh Muttaleb
Riyadh Muttaleb 2016년 8월 8일
댓글: Muhammad Usman Saleem 2017년 11월 29일
I have these data:
x=[1.6 1.1 .6 .1 .4 .9 1.4 1.9];
y=[1 1.5 2 2.5 3 3.5 4 4.5];
I would like to plot them smoothly w/o any angles.
I have used smooth function (like smooth(x)) but still I have angles.
Thanks in advance,
Riyadh

채택된 답변

Image Analyst
Image Analyst 2016년 8월 9일
With your data, my demo would become this:
% Create the original knot points.
xInitial = [1.6 1.1 .6 .1 .4 .9 1.4 1.9];
yInitial = [1 1.5 2 2.5 3 3.5 4 4.5];
lengthX = length(xInitial);
% flip sideways so there is only 1 y for every x
x = yInitial;
y = xInitial;
% Plot it and show how the line has sharp bends.
plot(x, y, '-sr', 'LineWidth', 2);
grid on;
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Use splines to interpolate a smoother curve,
% with 10 times as many points,
% that goes exactly through the same data points.
samplingRateIncrease = 10;
newXSamplePoints = linspace(min(x), max(x), lengthX * samplingRateIncrease);
smoothedY = spline(x, y, newXSamplePoints);
% Now flip back
ySmooth = newXSamplePoints;
xSmooth = smoothedY;
% Plot smoothedY and show how the line is
% smooth, and has no sharp bends.
hold on; % Don't destroy the first curve we plotted.
plot(newXSamplePoints, smoothedY, '-ob');
title('Spline Interpolation Demo', 'FontSize', 20);
legend('Original Points', 'Spline Points');
figure;
plot(xInitial, yInitial, '-sr', 'LineWidth', 2);
% Plot smoothedY and show how the line is
% smooth, and has no sharp bends.
hold on; % Don't destroy the first curve we plotted.
plot(xSmooth, ySmooth, '-ob');
grid on;
You can also use a Savitzky-Golay filter to smooth curves, and I attach a demo for that.
  댓글 수: 4
Image Analyst
Image Analyst 2017년 11월 24일
Not sure what you mean. If you have a problem with your data, then post your data.
Muhammad Usman Saleem
Muhammad Usman Saleem 2017년 11월 29일
Dear Sir;
Here is I my data and same problem Spline interpolation

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2016년 8월 8일
It is not possible to plot without any angles. Bit-mapped displaces have angles at every pixel, and vector displays are not able to support true curves. In MATLAB, ultimately every curve is approximated by straight lines or discretized into pixels.
What is possible is to create a line that appears to a be somewhat smooth curve, provided that a high enough density display is used.
My guess is that you want to use cubic spline interpolation to invent bogus intermediate points for the sake of disguising how sparse your justifiable data is. (I get kind of negative about the overuse of splines; not everyone agrees with me.)
  댓글 수: 1
Riyadh Muttaleb
Riyadh Muttaleb 2016년 8월 10일
Thank you for your comment

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 8월 9일
See my spline demo:
% Demo to show spline interpolation.
% Clean up / initialize
clc;
close all;
clear all;
workspace; % Display workspace panel.
% Create the original knot points.
lengthX = 10;
x = 1:lengthX;
y = rand (lengthX,1);
% Plot it and show how the line has sharp bends.
plot(x, y, '-sr', 'LineWidth', 2);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Use splines to interpolate a smoother curve,
% with 10 times as many points,
% that goes exactly through the same data points.
samplingRateIncrease = 10;
newXSamplePoints = linspace(1, lengthX, lengthX * samplingRateIncrease);
smoothedY = spline(x, y, newXSamplePoints);
% Plot smoothedY and show how the line is
% smooth, and has no sharp bends.
hold on; % Don't destroy the first curve we plotted.
plot(newXSamplePoints, smoothedY, '-ob');
title('Spline Interpolation Demo', 'FontSize', 20);
legend('Original Points', 'Spline Points');
% Mathworks Demo code from their Help
% x = 0:10;
% y = sin(x);
% xx = 0:.25:10;
% yy = spline(x,y,xx);
% plot(x,y,'o',xx,yy)
slopes = [0, diff(smoothedY)];
plot(newXSamplePoints, slopes, 'k-', 'LineWidth', 3);
% Draw x axis
line(xlim, [0,0], 'Color', 'k', 'LineWidth', 2);
grid on;
legend('Original Points', 'Spline Points', 'Slope');

카테고리

Help CenterFile Exchange에서 Splines에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by