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 visualización (últimos 30 días)
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 comentarios
Caylyn MacDougall
Caylyn MacDougall el 8 de Dic. de 2018
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')
Caylyn MacDougall
Caylyn MacDougall el 8 de Dic. de 2018
also with what i currently have, I find that using polyfit is more accurate. however im pretty sure interpolation is actually more accurate

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 8 de Dic. de 2018
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 comentarios
Caylyn MacDougall
Caylyn MacDougall el 10 de Dic. de 2018
@rik not @madhan. @madhan's answer may be more what im looking for, taking my last comment into context
Rik
Rik el 10 de Dic. de 2018
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by