Asked by Caylyn MacDougall
on 7 Dec 2018

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.')

Answer by Rik
on 8 Dec 2018

Accepted Answer

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');

Caylyn MacDougall
on 10 Dec 2018

Caylyn MacDougall
on 10 Dec 2018

Rik
on 10 Dec 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Rik (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/434520-i-am-trying-to-write-a-code-to-solve-a-simple-problem-as-shown-below-i-believe-what-am-i-am-doing-i#comment_647757

## Caylyn MacDougall (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/434520-i-am-trying-to-write-a-code-to-solve-a-simple-problem-as-shown-below-i-believe-what-am-i-am-doing-i#comment_647786

## Caylyn MacDougall (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/434520-i-am-trying-to-write-a-code-to-solve-a-simple-problem-as-shown-below-i-believe-what-am-i-am-doing-i#comment_647787

Sign in to comment.