MATLAB Answers

Unrecognized function or variable x

40 views (last 30 days)
Charlotte Massie
Charlotte Massie on 1 Apr 2020
Answered: M on 1 Apr 2020
Unrecognized function or variable 'x'.
Error in normalcaulculation (line 7)
k1 = E*x(1)/l(1,0)*[(cos(a(1)))^2 sin(a(1))*cos(a(1));sin(a(1))*cos(a(1)) (sin(a(1))^2)];
In the code:
F = 1000*[10000;-4000];%N
E = 70E9;%Pa = N/m2
l = [2 2/3^0.5 2^0.5];%m
a = [deg2rad(-30) deg2rad(-60) deg2rad(-135)];%rad
%bar 1 global matrix at node 4
k1 = E*x(1)/l(1,0)*[(cos(a(1)))^2 sin(a(1))*cos(a(1));sin(a(1))*cos(a(1)) (sin(a(1))^2)];
%bar 2 global matrix at node 4
k2 = E*x(2)/l(2)*[(cos(a(2)))^2 sin(a(2))*cos(a(2));sin(a(2))*cos(a(2)) (sin(a(2))^2)];
%bar 3 global matrix at node 4
k3 = E*x(3)/l(3)*[(cos(a(3)))^2 sin(a(3))*cos(a(3));sin(a(3))*cos(a(3)) (sin(a(3))^2)];
% nodal displacement in node 4
k = k1+k2+k3;
d4 = inv(k)*F;
%force component at each bar
f1 = k1*d4;
f2 = k2*d4;
f3 = k3*d4;
%axial stress
rf1 = sqrt(f1(1)^2+f1(2)^2);
rf2 = sqrt(f2(1)^2+f2(2)^2);
rf3 = sqrt(f3(1)^2+f3(2)^2);
Fo = [rf1 rf2 rf3]; %N/m2
X = [x(1) x(2) x(3)]; %m2
c = Fo-400E6*X;
%Equality constraint
ceq = []; %no equality constraint
S = Fo*X
function [c,ceq] = constraint(x)
% constraint: axial stress<=400N/m2
% x is the area of bar, x1=A1,x2=A2,x3=A3
% Load, geometry and materal
F = [10000e3;-2000e3];%unit change (kN--N)
E = 70E9;%Pa = N/m2
l = [2 2/sqrt(3) sqrt(2)];%m
a = [deg2rad(-30) deg2rad(-60) deg2rad(-135)];%rad
%bar 1 global matrix at node 4
k1 = E*x(1)/l(1)*[(cos(a(1)))^2,sin(a(1))*cos(a(1));sin(a(1))*cos(a(1)),(sin(a(1))^2)];
%bar 2 global matrix at node 4
k2 = E*x(2)/l(2)*[(cos(a(2)))^2,sin(a(2))*cos(a(2));sin(a(2))*cos(a(2)),(sin(a(2))^2)];
%bar 3 global matrix at node 4
k3 = E*x(3)/l(3)*[(cos(a(3)))^2,sin(a(3))*cos(a(3));sin(a(3))*cos(a(3)),(sin(a(3))^2)];
% nodal displacement in node 4
k = k1+k2+k3;
d4 = inv(k)*F; %P
%force component at each bar
f1 = k1*d4;
f2 = k2*d4;
f3 = k3*d4;
%axial force
s1 = (sqrt(f1(1)^2+f1(2)^2))/x(1);
s2 = (sqrt(f2(1)^2+f2(2)^2))/x(2);
S3 = (sqrt(f3(1)^2+f3(2)^2))/x(3);
%inequlity constraint
Fo = [s1 s2 S3]; %N/mm2
c = Fo-400;
%Equality constraint
ceq = []; %no equality constraint
end

  0 Comments

Sign in to comment.

Answers (1)

M
M on 1 Apr 2020
As the error message says, the variable 'x' is not declared before it is used line 7.
How do you execute this function ?
Is it your main script ?

  0 Comments

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!