Why am I getting this error?

L = 2;
W = 2;
Ra = (W*L*(1/2));
Ma = (W*(L+L*(2/3)));
x_true = linspace (0,2*L,51);
v = ones(1,length(x_true));
m = ones(1,length(x_true));
for a = 1: (length(x_true))
x = x_true(a);
if x<=L
v(a) = Ra;
m(a) = Ra*x - Ma;
elseif x<=(3/2)*L
v(a) = Ra - (((x-L)*(x-L))/2);
m(a) = Ra*(x-L) - (((x-2)^3)/6) - Ma;
else
v(a) = Ra - (x-L)*(2*L-x) + (((2*L-x)*(W-(x-L)))/2);
m(a) = -Ma + Ra(2*L-x) - ((x-L)*(2*L-x) + (((2*L-x)*(W-(x-L)))/2))*(((2*L-x)*(W + 2*x - L))/(3*(W + x - L)));
end
end
Array indices must be positive integers or logical values.
plot (x_true,v,'r');
title('Shear Force Diagram');
xlabel('Beam Length(m)');
ylabel('Shear (N)');
xlim ([0 2.5*L])
xticks ([0 1*L 1.5*L 2.5*L])
grid on
plot (x_true,m,'b');
title('Bending Moment Diagram');
xlabel('Beam Length(m)');
ylabel('Moment (N-m)');
xlim ([0 2.5*L])
xticks ([0 1*L 1.5*L 2.5*L])
grid on
Beam_Length=x_true.';
Shear=v.';
Bending_Moment=m.';
Shear_Moment_table=table(Beam_Length,Shear,Bending_Moment);
display(Ma)
display(Ra)

Réponses (2)

Dyuman Joshi
Dyuman Joshi le 20 Nov 2023

0 votes

There is a missing operator between Ra and (2*L-x) in the else statement block -
Update the code accordingly.
%% vvvvvvvv
m(a) = -Ma + Ra(2*L-x) - ((x-L)*(2*L-x) + (((2*L-x)*(W-(x-L)))/2))*(((2*L-x)*(W + 2*x - L))/(3*(W + x - L)));
Also, it would be better to defined the number of points used to define the variable x_true by linspace() and use that variable instead of calling length() everytime.

3 commentaires

Thank you so much, I totally missed the *. Also, I have to keep the length in terms of L and x because of the type of problem. This is supposed to represent a V and M diagram of a beam using the method of sections.
I changed my code to this, and the moment graph that it's giving me is almost right, its just the middle section (between 2<x< 3) that is wrong. I attached a picture of what the graph is supposed to look like. Any idea why it's not working?
L = 2;
W = 2;
Ra = (W*L*(1/2));
Ma = (W*(L+L*(2/3)));
x_true = linspace (0,2*L,51);
v = ones(1,length(x_true));
m = ones(1,length(x_true));
for a = 1: (length(x_true))
x = x_true(a);
if x<=L
v(a) = Ra;
m(a) = Ra*x - Ma;
elseif x<=(3/2)*L
v(a) = Ra - (((x-L)*(x-L))/2);
m(a) = Ra*(x-L) + (((x-2)^3)/6) ;
else
v(a) = Ra - (x-L)*(2*L-x) + (((2*L-x)*(W-(x-L)))/2);
m(a) = - Ra*(2*L-x) + ((x-L)*(2*L-x) + (((2*L-x)*(W-(x-L)))/2))*(((2*L-x)*(W + 2*x - L))/(3*(W + x - L)));
end
end
plot (x_true,v,'r');
title('Shear Force Diagram');
xlabel('Beam Length(m)');
ylabel('Shear (N)');
xlim ([0 2.5*L])
xticks ([0 1*L 1.5*L 2.5*L])
grid on
plot (x_true,m,'b');
title('Bending Moment Diagram');
xlabel('Beam Length(m)');
ylabel('Moment (N-m)');
xlim ([0 2.5*L])
xticks ([0 1*L 1.5*L 2.5*L])
grid on
Beam_Length=x_true.';
Shear=v.';
Bending_Moment=m.';
Shear_Moment_table=table(Beam_Length,Shear,Bending_Moment);
display(Ma)
display(Ra)
Dyuman Joshi
Dyuman Joshi le 20 Nov 2023
Could you please share the description and any related information of the problem you are trying to solve?
AStudent
AStudent le 20 Nov 2023
For L I chose 4m, for Wo I made it 2kN-m.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Conway's Game of Life dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by