Pass variables between functions

I have coded a script which should return the stiffness matrix (not done yet!) and I wonder how I can pass a matrix between various functions? (I don't want to type a giant matrix as an input parameter)
When I run it, it complains on the PB(Bmatrix,D) and KMatrix(D,Areaterm).
Thank you for your help!
clc
clear all
%% Creating matrices holding some values for nodes and elements:
% Define Node Coordinates - change these values!
ConnectingNodes=[0.425 0;
0.525 0;
0.525 0.1;
0.425 0.1;
0.475 0.05];
% Connecting elements with 5 nodes:
ConnectingElements=[1 2 5;
2 5 3;
3 5 4;
1 4 5];
%% Improvements:
% Creating if-else for the number of nodes (triangle)
% Ask the user for input i.e number of nodes etc..
%% Creating a nested for-loop for printing both the elements and nodes:
% Marking the nodes:
for a=1:length(ConnectingNodes)
%Marking the elements:
for b=1:length(ConnectingElements)
r1=ConnectingNodes(ConnectingElements(b,1),1);
z1=ConnectingNodes(ConnectingElements(b,1),2);
r5=ConnectingNodes(ConnectingElements(b,2),1);
z5=ConnectingNodes(ConnectingElements(b,2),2);
r6=ConnectingNodes(ConnectingElements(b,3),1);
z6=ConnectingNodes(ConnectingElements(b,3),2);
plot([r1 r5 r6 r1],[z1 z5 z6 z1],'-black')
[r_hat,z_hat]=centroid(polyshape([r1 r5 r6],[z1 z5 z6]));
disp("Elements:" + length(ConnectingElements))
%% Assembling the stiffness matrix:
end
plot(ConnectingNodes(a,1),ConnectingNodes(a,2))
text(ConnectingNodes(a,1),ConnectingNodes(a,2),num2str(a))
disp("Nodes:" + length(ConnectingNodes))
hold on
end
%% Calling our functions:
%Parameters:
param(1,0,1.5,0.5,2,0)
%D-matrix:
D(210*10^9 , 0.3)
%Area:
AreaTerm(1,1.5,0,0,0.5,0)
%% Issues here!
%B-matrix:
PB(Bmatrix,D)
%K-matrix:
KMatrix(D,AreaTerm)
%% End of issues
% Declaring variables and size of cylinder:
p_rand = 3;
p = 60*10^6;
di = 850;
do = 1050;
L = 3*do;
%Nodal force:
NodalForce(p,p_rand,di,do,L)
%% Check here variable parsing - errors can be found here!
%% Parameter adjustment
function variable1 = param(r1,z1,r5,z5,r6,z6)
a1 = (r5*z6) - (z5*r6);
a5 = (r6*z1) - (z6*r1);
a6 = (r1*z5) - (z1*r5);
B1 = z5-z6;
B5 = z6-z1;
B6 = z1-z5;
Gamma1 = r6-r5;
Gamma5 = r1-r6;
Gamma6 = r5-r1;
% Doesn't do anything except printing a 3x3 matrix with our variables:
variable1 = [a1 a5 a6; B1 B5 B6; Gamma1 Gamma5 Gamma6];
disp("variables:")
disp(variable1)
end
%% Area:
function Area = AreaTerm(r1,r5,r6,z1,z5,z6)
%[rbar,zbar]=centroid(polyshape([ri rj rm],[zi zj zm]));
Area=area(polyshape([r1 r5 r6],[z1 z5 z6]));
disp("Area:" + Area)
end
%% D-matrix:
function DMatrix = D(E,v)
DMatrix = E/((1+v)*(1-(2*v)))*...
[(1-v) v v 0;
v (1-v) v 0;
v v (1-v) 0;
0 0 0 ((1-(2*v))/2)];
disp("D-matrix:")
disp(DMatrix)
end
%% B-Matrix:
function BMat = Bmatrix(Area,B1,B5,B6,Gamma1, Gamma5, Gamma6, z_hat,r_hat)
BMat = (1/(2*Area)).*[B1 0 B5 0 B6 0;
0 Gamma1 0 Gamma5 0 Gamma6;
((a1/r_hat) + B1 + (Gamma1*z_hat/r_hat)) 0 ((a5/r_hat) + B5 + (Gamma5*z_hat/r_hat))...
0 ((a6/r_hat) + B6 + (Gamma6*z_hat/r_hat)) 0;
Gamma1 B1 Gamma5 B5 Gamma6 B6];
disp("B-Matrix:")
disp(BMat)
end
%% Product of the B and D matrix:
function ProdBD = PB(BMat,DMatrix)
BTrans = BMat';
ProdBD = BTrans*DMatrix;
disp("ProdBD:")
disp(ProdBD)
end
%% Nodal forces:
function NF = NodalForce(p,p_rand,di,do,L)
NF = pi*p_rand*L/p;
disp("NF:")
disp(NF)
end

回答 (1 件)

Torsten
Torsten 2023 年 1 月 20 日

0 投票

If you type
PB(Bmatrix,D)
then Bmatrix and D are inputs to the function PB. Thus they must somehow be defined before this call.
This in not the case in your code.

カテゴリ

ヘルプ センター および File ExchangeGamma Functions についてさらに検索

タグ

質問済み:

2023 年 1 月 20 日

回答済み:

2023 年 1 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by