I need to make a plot using spline interpolation?
16 ビュー (過去 30 日間)
古いコメントを表示
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.

0 件のコメント
採用された回答
Star Strider
2015 年 4 月 23 日
Start by creating two vectors, ‘Force’ and ‘Elongation’ out of the data you are provided. The documentation for the interp1 function has all the information you need.
Not to give away the answer, but remember that the rubber specimen has an initial length, and you are to find the force an a specified greater total length, so make allowances for that.
1 件のコメント
Star Strider
2015 年 4 月 23 日
You had your independent and dependent variables backwards. You want force as a function of length, not the inverse. With that correction, the code almost writes itself.
I don’t know where ‘yfun’ came from, but you don’t need it for this problem.
This is how I would do it:
F = [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];
E = [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];
xi=linspace(min(E), max(E), 50);
yispl=interp1(E,F,xi,'spline');
rest_length = 3;
total_length = 11.5;
stretch_length = total_length - rest_length;
ystretch=interp1(E,F,stretch_length,'spline');
subplot(1,3,1)
plot(E,F,'o', xi,yispl,'--')
hold on
plot(stretch_length, ystretch, 'gp', 'MarkerSize',15)
hold off
grid
xlabel('Stretched Length (in)')
ylabel('Force ()')
fprintf(1, '\n\tForce at total length = %.1f" (stretched length = %.1f") = %.2f\n\n', total_length, stretch_length, ystretch)
It’s also best not to use generic variables such as ‘x’ and ‘y’ when descriptive ones such as ‘E’ and ‘F’ will convey more information and decrease the opportunity to confuse them. A small point, but one that becomes easy to appreciate in complicated code.
その他の回答 (3 件)
Image Analyst
2015 年 4 月 23 日
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 件のコメント
Image Analyst
2015 年 4 月 23 日
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
2015 年 4 月 23 日
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 件のコメント
Rania
2025 年 2 月 3 日
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Spline Postprocessing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!