How to plot discrete points in matlab

1 Ansicht (letzte 30 Tage)
Rahul Krishna H
Rahul Krishna H am 6 Feb. 2018
Bearbeitet: Walter Roberson am 6 Jun. 2025
I am working on a matlab code to plot mass time graph. On x axis time is mentioned and on yaxis mass is mentioned. x and y have discrete values and their values are the followings: x = 0 5 10 15 20 25 30 35 40 45 and y = 0.008 0.007 0.006 0.004 0.003 0.002 0.001 0.001 0.001 0.001 .When i plot is matlab i dont get a continoous curve rather i get a straight line curve as shown below.
https://www.mathworks.com/matlabcentral/answers/uploaded_files/103676/untitled.jpg The graph i obtained when plotted using microsoft excel is as follows
https://www.mathworks.com/matlabcentral/answers/uploaded_files/103677/18.JPG The graph obtained in excel is what i needed...Can we get the same graph as excel in matlab...my source code is attached here with:
function [] = a()
clc
x=[0 300 600 900 1200 1500 1800 2100 2400 2700];
y=[8 7 6 4 3 2 1 1 1 1];
plot(x,y,'--')
axis([0 3000 0 inf]);
title('Mass-Time Evaporation Analysis graph' )
xlabel('Time of evaporation in seconds')
ylabel('Mass of blood evaporated in kg')
end end

Antworten (1)

Altaïr
Altaïr am 6 Jun. 2025
By default, MATLAB's plot function generates piecewise linear segments. To create a smoother curve, interpolation between discrete data points can help. MATLAB offers several interpolation methods:
  • PCHIP/Cubic: Shape-preserving piecewise cubic (smooth, maintains monotonicity)
  • Spline: Cubic spline interpolation (smooth but may overshoot)
  • Makima: Modified Akima cubic interpolation (balances smoothness and shape)
Here's an example using PCHIP interpolation to generate a smooth curve alongside the original data:
% Original data (time in seconds, mass in kg)
x = [0, 300, 600, 900, 1200, 1500, 1800, 2100, 2400, 2700];
y = [0.008, 0.007, 0.006, 0.004, 0.003, 0.002, 0.001, 0.001, 0.001, 0.001];
% Create fine grid for smooth interpolation
x_fine = linspace(0, 2700, 1000); % 1000 points between 0-2700 seconds
y_fine = interp1(x, y, x_fine, 'pchip'); % Cubic interpolation
% Plot settings
plot(x_fine, y_fine, 'b-', 'LineWidth', 1.5); % Smooth blue curve
hold on;
plot(x, y, ':r', 'LineWidth', 1.5); % Piecewise linear curve
grid on;
% Axis and labels
axis([0, 3000, 0, 0.009]); % Extend x-axis to 3000 seconds
title('Mass-Time Evaporation Analysis');
xlabel('Time of evaporation (seconds)');
ylabel('Mass of blood evaporated (kg)');
legend('Interpolated Curve', 'Default Curve', 'Location', 'best');
hold off;
The interpolation creates 1000 refined points between measurements, generating a continuous curve while preserving the data's original shape.
For more interpolation options, explore the documentation:
doc interp1

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by