I need to make a plot using spline interpolation?
Afficher commentaires plus anciens
This is the problem I'm having trouble with. I really don't know where to start with it, so if someone could get me some pointers that would be amazing.

Réponse acceptée
Plus de réponses (3)
Image Analyst
le 23 Avr 2015
Here's 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)

1 commentaire
Image Analyst
le 23 Avr 2015
It's not the interp1() function like your homework asked for but the concept is pretty similar, and perhaps this demo will help someone.
Image Analyst
le 23 Avr 2015
Your code is very close, but basically you switched x and y. Try it this way - I just switched x and y and fancied up the plot a bit.
% Create sample data training points.
xElongation = [0 1.2 2.4 3.6 4.8 6.0 7.2 8.4 9.6 10.8 12.0 13.2 14.4 15.6 16.8 18];
yForce = [0 0.6 0.9 1.16 1.18 1.19 1.24 1.48 1.92 3.12 4.14 5.34 6.22 7.12 7.86 8.42];
% Plot "training points".
plot(xElongation, yForce, 'ro', 'LineWidth', 2);
grid on;
hold on;
fontSize = 20;
xlabel('Elongation', 'FontSize', fontSize);
ylabel('Force', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Do a fit everywhere. Use, say, 200 points.
xFit = linspace(xElongation(1), xElongation(end), 200);
yFittedForce = interp1(xElongation, yForce, xFit, 'spline');
% Plot the fit
plot(xFit, yFittedForce, 'b.-', 'MarkerSize', 5);
% Find the force when elongation = 11.5
y115 = interp1(xElongation, yForce, 11.5, 'spline');
message = sprintf('The Force at 11.5 is %f', y115);
uiwait(helpdlg(message));

3 commentaires
Image Analyst
le 23 Avr 2015
Because it started at 3 inches, and if elongation is the length beyond 3 inches, then you might need to find the value at an x value of 8.5, because 3 + 8.5 = 11.5. As you can see it's a trivial change.
Elise M.
le 23 Avr 2015
Elise M.
le 23 Avr 2015
Rania
le 3 Fév 2025
0 votes
x = [1.8 2.7 4.5 5.2 7.1 8.5]; y = [4 10.1 11.5 10.2 8.4 8.6]; xx = 5.5:1:5.5 yy = spline(x,y,xx) plot(x,y,'o',xx,yy) Use this function for calculation any spline.
Catégories
En savoir plus sur Spline Postprocessing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!