What is wrong with my code

1 view (last 30 days)
Daniel Sode
Daniel Sode on 20 Jul 2020
Answered: Dinesh Yadav on 23 Jul 2020
N = 16;
E = 200e9; % Young Modulus (Pa)
b = 0.1; % Top width (m^2)
a = 0.025; % Bottom width (m^2)
L = 1.0; % Length (m)
rho = 77e3; % Force density (N/m^3)
Le = L./N;
ff = 0;
for i = 1:N
w(i)=(((i-0.5).*Le)*((b-a)./L))+a; %Calculates avg width (m)
A(i)=w(i).^2; %Calculates element area (m^2)
k(i) = A(i)*E/Le; %Local Stiffness Matrix constact
f(i) = ff*A(i)*le*rho/2; %Local Force Matrix constant
end
KG = zeros(N+1,N+1)
FG = zeros(N+1,N+1)
for ii = 1:N
KG(ii:ii+1,ii:ii+1)=KG(ii:ii+1)+k(i);
FG(ii:ii+1,ii:ii+1)=FG(ii:ii+1)+f(i);
end
Displacement = inv(KG)*FG;
plot(N,Displacement,'b');
hold on
plot(N,Displacement,'*','color','r')
ylabel('end deflection in mm');
xlabel('no of element')
  5 Comments
Daniel Sode
Daniel Sode on 20 Jul 2020
I redid the code and am getiing this error
Unable to perform assignment because the left and right sides have a different number of elements.
Error in Termproject (line 10)
w(i)=(((i-0.5).*Le)*((b-a)./L))+a; %Calculates avg width (m)
Daniel Sode
Daniel Sode on 20 Jul 2020
N = linspace(1,16,100);
E = 200e9; % Young Modulus (Pa)
b = 0.1; % Top width (m^2)
a = 0.025; % Bottom width (m^2)
L = 1.0; % Length (m)
rho = 77e3; % Force density (N/m^3)
Le = L./N;
ff = 0;
for i = 1:N
w(i)=(((i-0.5).*Le)*((b-a)./L))+a; %Calculates avg width (m)
A(i)=w(i).^2; %Calculates element area (m^2)
k(i) = (A(i)*E/Le);%*[1 -1;-1 1]; %Local Stiffness Matrix constact
f(i) = (ff*A(i)*Le*rho/2);%*[1;1]; %Local Force Matrix constant
end
KG = zeros(N+1,N+1);
FG = zeros(N+1,N+1);
for i = 1:N
KG(i,i) = KG(i,i) + k(i);
KG(i,i+1) = KG(i,i+1) - k(i);
KG(i+1,i) = KG(i+1,i) - k(i);
KG(i+1,i+1) = KG(i+1,i+1) + k(i);
FG(i,1) = FG(i,1) + f(i);
FG(i+1,1) = FG(i+1,1) +f(i)
end
Displacement = inv(KG)*FG;
plot(N,Displacement,'b');
hold on
plot(N,Displacement,'*','color','r')
ylabel('end deflection in mm');
xlabel('no of element')

Sign in to comment.

Answers (1)

Dinesh Yadav
Dinesh Yadav on 23 Jul 2020
Change to the following code
len = length(N) %N is 1x100 you want i to go from 1 to 100 therefore change to length(N) instean of N
for i = 1:len
w(i)=(((i-0.5)*Le(i))*((b-a)/L))+a; % Le is also 1x100 you want to take one elsment at a time use Le(i)
A(i)=w(i).^2; %
k(i) = (A(i)*E/Le(i));%*[1 -1;-1 1]; %
f(i) = (ff*A(i)*Le(i)*rho/2);%*[1;1]; %
end
KG = zeros(len+1,len+1);
FG = zeros(len+1,len+1);
for i = 1:len

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!