MATLAB: Error in plotting slope and deflection graph

graph

clear;
clc;
% User enter the corresponding values
E=input('Enter the value of Young Modulus E(Pa):')
I=input('Enter the value of Moment of Inertia I(m^4):')
w=input('Enter the value of uniform distributed load w(k N/m):')
a=input('Enter the value of a(m):')
L=input('Enter the value of total length of beam L(m):')
Va= ((w)*(L-a))-((w)*(a)); %To calculate the force at fixed support
Ma= (((w)*(L-a)*((L-a)/2))-((w)*(a)*((L-a)+(a/2)))-((Va)*(L))); %To calculate the moment at fixed support
for x=1:0.1:L %To calculate the slope and deflection of beam AB from x=0 to x=total length of beam AB
fprintf("\nWhen x=%.1f", x)
if (x-a)<0
slope= (1000/(E*I))*((Ma*x)+((Va/2)*(x^2))+((w/6)*(x^3))); %To calculate the slope of beam

fprintf("\nThe slope of beam AB is %.5frad", slope)
deflection= (1000/(E*I))*(((Ma/2)*(x^2))+((Va/6)*(x^3))+((w/24)*(x^4))); %To calculate the deflection of beam

fprintf ("\nThe deflection of beam AB is %.5fm\n", deflection)
else
slope= (1000/(E*I))*((Ma*x)+((Va/2)*(x^2))+((w/6)*(x^3))-((w/3)*((x-a)^3))); %To calculate the slope of beam
fprintf("\nThe slope of beam AB is %.5frad", slope)
deflection= (1000/(E*I))*(((Ma/2)*(x^2))+((Va/6)*(x^3))+((w/24)*(x^4))-((w/12)*(((x-a)^4)))); %To calculate the deflection of beam
fprintf ("\nThe deflection of beam AB is %.5fm\n", deflection)
end
end
plot(x,slope)
plot(x,deflection)

Best Answer

by You need to save slope and deflection inside the loop and plot. Actually you need not to use loop for this.
clc; clear ;
% User enter the corresponding values
E=input('Enter the value of Young Modulus E(Pa):')
I=input('Enter the value of Moment of Inertia I(m^4):')
w=input('Enter the value of uniform distributed load w(k N/m):')
a=input('Enter the value of a(m):')
L=input('Enter the value of total length of beam L(m):')
Va= ((w)*(L-a))-((w)*(a)); %To calculate the force at fixed support
Ma= (((w)*(L-a)*((L-a)/2))-((w)*(a)*((L-a)+(a/2)))-((Va)*(L))); %To calculate the moment at fixed support
X=1:0.1:L ;
y = zeros(size(X)) ;
d = zeros(size(X)) ;
for i = 1:length(X) %To calculate the slope and deflection of beam AB from x=0 to x=total length of beam AB
x = X(i) ;
fprintf("\nWhen x=%.1f", x)
if (x-a)<0
slope= (1000/(E*I))*((Ma*x)+((Va/2)*(x^2))+((w/6)*(x^3))); %To calculate the slope of beam

fprintf("\nThe slope of beam AB is %.5frad", slope)
deflection= (1000/(E*I))*(((Ma/2)*(x^2))+((Va/6)*(x^3))+((w/24)*(x^4))); %To calculate the deflection of beam

fprintf ("\nThe deflection of beam AB is %.5fm\n", deflection)
else
slope= (1000/(E*I))*((Ma*x)+((Va/2)*(x^2))+((w/6)*(x^3))-((w/3)*((x-a)^3))); %To calculate the slope of beam
fprintf("\nThe slope of beam AB is %.5frad", slope)
deflection= (1000/(E*I))*(((Ma/2)*(x^2))+((Va/6)*(x^3))+((w/24)*(x^4))-((w/12)*(((x-a)^4)))); %To calculate the deflection of beam
fprintf ("\nThe deflection of beam AB is %.5fm\n", deflection)
end
y(i) = slope ;
d(i) = deflection ;
end
figure
plot(X,y
figure
plot(X,d)