index exceeds the number of array elements

I try to plot the deflection of a simply supported beam using rk4 with the following initial conditions
y(0)=0 and y (L)=0
L= length of beam
close all
clear all
clc
%constants
E=20*10^9;
w=0.05;
h=0.1;
I= w*h^3/12;
L=5;
q=1500*L;
% converting 2nd ODE into two 1st ODE
% y''=(-q/2.*(x.^2-L.*x))/EI
% let y'=d , y(0)=0;
% define function handles
%y=(x, d) <= y(1,:)=y y(2,:)=d
Y= @(x,y) [ ...
y(2);
(-q/2.*(x.^2-L.*x))/(E*I)];
%stepsize
Length=5;
h=0.0001;
n=Length/h;
% initial conditions
% at position x=0 and x=L, position is fixed and thus y(1,1)
x(1)=0;
y(1,1)=0;
y(1,n)=0;
%loop
for i= 1:n
%timeloop
x(i+1)= x(i)+h;
%rk loop
k1= Y(x(i) ,y(:,i) );
k2= Y(x(i)+0.5.*h,y(:,i)+0.5.*k1.*h);
k3= Y(x(i)+0.5.*h,y(:,i)+0.5.*k2.*h);
k4= Y(x(i)+ h,y(:,i)+ k3.*h);
y(:,i+1)= y(:,i)+h.*(k1+2*k2+2*k3+k4)/6;
end
figure
plot (x, y(1,:))
title('question6')
hold on
plot (x, y(2,:))
hold on
a1=plot (x, y(1,:)) ; M1="distance vs position";
a2=plot (x, y(2,:)) ; M2="distance vs deflection";
legend([a1, a2],[M1, M2])
hold off

Answers (1)

The error message is telling you the issue. Somewhere in your code, you are indexing an array using an index value greater than the length of the array.
A=1:5;
% This works
a=A(5)
a = 5
% This does not
b=A(8)
Index exceeds the number of array elements (5).

6 Comments

I try to figure out but still it is not clear which part is incorrect
The error message (red text) you are getting should be telling you which line is causing the problem. Try copying and pasting the full message (all the red text) here so we can help.
this is the error I get
Index exceeds the number of array elements (1).
Error in question_7>@(x,y)[y(2);(-q/2.*(x.^2-L.*x))/(E*I)] (line 19)
y(2);
Error in question_7 (line 43)
k1= Y(x(i) ,y(:,i) );
Y is an anonymous function (defined in line 19) with 2 inputs, x and y. When you call it in line 43, your input for x is x(i) and your input for y is y(:,i).
The error occurs inside your anonymous function, specifically when you try to extract the 2nd value from the y input: y(2). However, y is a row vector, so your input of y(:,i) is only ever a single number since there is only one row in y.
Perhaps check how you are creating y. Also, consider using different variable names so that it is more clear where each one is used.
%constants
E=20*10^9;
w=0.05;
h=0.1;
I= w*h^3/12;
L=5;
q=1500*L;
Y= @(xin,yin) [ yin(2); (-q/2.*(xin.^2-L.*x))/(E*I)];
%stepsize
Length=5;
h=0.0001;
n=Length/h;
% initialize
x = 0;
y = zeros(1,n);
i=1;
x(i)
ans = 0
y(:,i)
ans = 0
% Error here because yin(2) exceeds the size of yin, which is 1x1
k1= Y(x(i) ,y(:,i) );
Index exceeds the number of array elements (1).

Error in solution (line 9)
Y= @(xin,yin) [ yin(2); (-q/2.*(xin.^2-L.*x))/(E*I)];
I'm still not sure if this method (rk4) is appropriate to simulate the deflection of a simply supported beam
which has this deflection formula d2y/dx2=(-q/2*(x^2-L*x))/(2*E*I)
I do know that centered finited difference method (CFD) works well for this problem but I don't know how to use CFD to reduce the problem in matrixes and obtain the solution.
I suspect the issue is with your implementation of the runge-kutta method. That is a different question than the one asked here. You could search this forum to find hundreds of answers on implementing it.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2020b

Tags

Asked:

on 29 Mar 2021

Commented:

on 29 Mar 2021

Community Treasure Hunt

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

Start Hunting!