Vectors Are Not Lining Up

조회 수: 1 (최근 30일)
Claire
Claire 2015년 3월 26일
댓글: Star Strider 2015년 3월 26일
Hello,
I am having a bit of difficulty with this problem. When I run my code I get an error saying that my vectors are not the same size. Any help is greatly appreciated. The code is provided below.
function ydd=SecDeriv(x,y)
% Calculate the size of the array x
n = length(x);
% Calculate the spacing of the data h
h = x(2)-x(1);
% Second derivative at first point of the array x: 4-point forward difference formula - Error = O(h^2)
ydd(1) = (2*y(1)-5*y(2)+4*y(3)-y(4))/h/h;
% Second derivative at last point of the array x: 4-point backward difference formula - Error = O(h^2)
ydd(n) = (y(n-3)-4*y(n-2)+5*y(n-1)-2*y(n))/h/h;
% Second derivative at the intermediate points: three point central difference formula - Error = O(h^2)
for i=2:n-1
ydd(i) = (y(i-1)-2*y(i)+y(i+1))/h/h;
end
end
This is then used in this script file:
% Plot the exact bending moment and the approximate bending moment
% on the same set of axes
x = [0 24 48 72 96 120 144 168 192 216 240 264 288 312 336 360];
y = [0 -0.111 -0.216 -0.309 -0.386 -0.441 -0.473 -0.479 -0.458 -0.412 -0.345 -0.263 -0.174 -0.090 -0.026 0];
L = 360; E = 29E6; I = 720; q0 = 250;
x1 = linspace(0,L,101);
Mexact = -(q0*L^4/120)*((20/L^2)*(x./L).^3-(12/L^2)*(x./L));
M = E*I*SecDeriv(x,y);
plot(x,M,'*',x1,Mexact,'--');xlabel('x (in)');ylabel('Moment (in-lb)');
legend('Mexp','Mexact');title('Bending Moment');
I am having trouble, I believe, with the line Mexact = -(q0*L^4/120)*((20/L^2)*(x./L).^3-(12/L^2)*(x./L));

채택된 답변

Star Strider
Star Strider 2015년 3월 26일
Guessing wildly here, but since you want to plot ‘Mexact’ as a function of ‘x1’, you might want to calculate it using ‘x1’ instead of ‘x’:
Mexact = -(q0*L^4/120)*((20/L^2)*(x1./L).^3-(12/L^2)*(x1./L));
  댓글 수: 2
Claire
Claire 2015년 3월 26일
Yes, you are exactly right. Thank you!
Star Strider
Star Strider 2015년 3월 26일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by