It is difficult to control the range

2 ビュー (過去 30 日間)
재훈
재훈 2024 年 5 月 16 日
コメント済み: 재훈 2024 年 5 月 21 日
Hellow there. I want to connect functions with different scopes, but it doesn't work. Where did I go wrong? Have a nice day everyone
here is my code
clc; clear all; close all;
A = readmatrix('w10_data.xlsx','Sheet','Sheet1');
x = A(:,1);
fx = A(:,2);
%1D linear interpolation
xspace = [];
px = [];
for i = 1:3
x_segment = linspace(x(i), x(i+1),101);
p_segment = fx(i) + (fx(i+1) - fx(i))/(x(i+1)-x(i))*(x_segment-x(i));
xspace = [xspace x_segment(1:end-1)];
px = [px p_segment(1:end-1)];
end
% Polynomial interpolation
xspace2 = 4:0.01:7;
Lx = zeros(1,length(xspace2));
for j = 1:size(x,1)
lj = 1;
for m = 1:size(x,1)
if m == j
continue
end
lj = lj .*(xspace2 -x(m))/((x(j)-x(m)));
end
Lx = Lx + fx(j)*lj;
end
% Cubic spline interpolation
xspace3 = 7:0.01:10;
S = spline(x(7:end),fx(7:end),xspace3);
%S = spline(x,fx,xspace2);
figure(1)
scatter(x,fx,'k','filled'); hold on;
plot(xspace,px,'b'); hold on;
plot(xspace2,Lx,'r'); hold on;
plot(xspace2,S,'g'); hold on;
xlim([0 12]);
ylim([0 20]);

採用された回答

Pratyush Swain
Pratyush Swain 2024 年 5 月 16 日
Hi 재훈,
From the code snippet you have provided , I can make out an observation as follows:
  • Plotting Issue with Cubic Spline: The line "plot(xspace2,S,'g')" seems to be incorrect because "xspace2" is used for polynomial interpolation, not for cubic spline interpolation. You probably meant to plot "xspace3" against "S" for the cubic spline part:
plot(xspace2,Lx,'r'); hold on;
% Correcting the following line
% plot(xspace2,S,'g'); hold on;
plot(xspace3,S,'g'); hold on;
% -----------------------------
After making the above change, I am getting the plot figure as follows:
For more information please refer to: https://www.mathworks.com/help/matlab/ref/spline.html
Hope this is what you were looking for.
  2 件のコメント
재훈
재훈 2024 年 5 月 16 日
Oh my mistake. Thank you for your help. Have a nice day! :)
Image Analyst
Image Analyst 2024 年 5 月 16 日
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2024 年 5 月 16 日
See my attached splines demos.
  1 件のコメント
재훈
재훈 2024 年 5 月 21 日
Sorry for checking late. Nonetheless, it helped me a lot. Thank you for your help. have a good day!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpline Postprocessing についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by