MATLAB Answers

0

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?

Asked by Caylyn MacDougall on 7 Dec 2018 at 23:34
Latest activity Commented on by Rik Wisselink
on 10 Dec 2018 at 7:26
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 Comments

I don't see any obvious mistakes, so if it runs without error, I don't see why this would be wrong.
You could also interpolate to a finer grid, to see what happens between values, to get a better idea of what the difference between the two methods is.
It does run without error, however my biggest concern i suppose is whether i should be wruting interp1(Dd,c,'spline') rather than interp1(c,Dd,'spline')
also with what i currently have, I find that using polyfit is more accurate. however im pretty sure interpolation is actually more accurate

Sign in to comment.

Products


Release

R2018a

1 Answer

Answer by Rik Wisselink
on 8 Dec 2018 at 9:29
 Accepted Answer

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 Comments

actually, would this syntax not just essentially be returning the same values for c and a as they are provided since the 'xi' we are interpolating at is the same original x vector provided, Dd. This seems redundant. Also am I correct in my statement that the spline fitting method is more accurate?
@rik not @madhan. @madhan's answer may be more what im looking for, taking my last comment into context
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.

Sign in to comment.