# I am trying to write a code to solve a simple problem as shown below. I believe what am I am doing is correct up until i begin the spline fitting method. Am i interpolating correctly?

1 ビュー (過去 30 日間)
Caylyn MacDougall 2018 年 12 月 7 日
コメント済み: Rik 2018 年 12 月 10 日
Dd=[6 3 2 1.5 1.2 1.1 1.07 1.05 1.03 1.01];
c=[0.88 0.89 0.91 0.94 0.97 0.95 0.98 0.98 0.98 0.92];
a=[0.33 0.31 0.29 0.26 0.22 0.24 0.21 0.2 0.18 0.17];
ktoriginal=(c.*((0.5.*Dd)-0.5).^(-a)); %stress concentration factor for a stepped circular shaft
%based on table
%1)obtain an expression for c(Dd) annd a(Dd) with a 5th order polynomial
c5fitpoly = polyfit(Dd,c,5); %polyfit finds coefficents of a polynomial of degree 5 that fits a as a function of Dd
a5fitpoly = polyfit(Dd,a,5);
%next use polyval to determine the values for a and c using the coefficient
%vectors found for each
c5=polyval(c5fitpoly,Dd); %evalutes coefficent vector at each Dd
a5=polyval(a5fitpoly,Dd);
%calculating new kt values from fitted data
ktfitpoly5=(c5.*((0.5.*Dd)-0.5).^(-a5));
%comparing this kt with the original kt
polyerror=abs(ktoriginal-ktfitpoly5)';
%2)obtain an expression for c(Dd) annd a(Dd) with spline interpolation
cspline=interp1(c,Dd,'spline');
aspline=interp1(a,Dd,'spline');
ktspline=(cspline.*((0.5.*Dd)-0.5).^(-aspline));
splineerror=abs(ktoriginal-ktspline)';
disp('The error of kt using the polyfit method gives an error of:')
disp(polyerror)
disp('for each kt, whereas the error of kt using the spline method gives an error of:')
disp(splineerror)
disp('for each kt. In conclusion the first method is better to use as the errors are smaller.')
##### 3 件のコメント表示非表示 2 件の古いコメント
Caylyn MacDougall 2018 年 12 月 8 日
also with what i currently have, I find that using polyfit is more accurate. however im pretty sure interpolation is actually more accurate

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

### 採用された回答

Rik 2018 年 12 月 8 日
I just read the documentation page for interp1, and it turns out you are using this syntax:
vq = interp1(v,xq,method)
What you should be using is this:
vq = interp1(x,v,xq,method)
So for your code that means this:
%2)obtain fitted values for c(Dd) annd a(Dd) with spline interpolation
cspline=interp1(Dd,c,Dd,'spline');
aspline=interp1(Dd,a,Dd,'spline');
##### 5 件のコメント表示非表示 4 件の古いコメント
Rik 2018 年 12 月 10 日
It is indeed a bit redundant. The reason I wrote it like this is because you have a check at the end that depends on having the true value as well. If you don't, then you can indeed use a finer interpolation, as I suggested and as Madhan wrote.

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

R2018a

### Community Treasure Hunt

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

Start Hunting!