Error using 2 Functions in the script
2 views (last 30 days)
Show older comments
Hello , I want with the following code to Plot simulations of a flexible wing in the time domain and frequency domain. What my objective is is to create two functions as can been seen (which basicly am calling a function into another function) for making the script shorter and and using the function for both cases - for time domain whch is for one certain Velocity (U_sim ) and for frequency domain which is a variety of speed . ODe45 solver and eig comands are used respectively to obtain displacement velocity response (time domain ) and mode shapes , damping ratio (Frewuency domain). When everything is on the main script it works fine . Can you please tell me what i am doing wrong?
lc
clear
close all
global s c x_f m e EI GJ rho M_theta a_w A_inv C E B D U_sim ZN V_air_i IN
% Setting Parameters
s=7.5; %semi span (m)
c=2; %chord (m)
x_f=(0.48*c); %Flexural axis (m)
m=100; %mass per unit area (kgm^-2)
e=0.23; %Eccentricity Ratio
EI=3675000; %Flexural rigitidy (Nm^2)
GJ=1890000 ;%Torsional rigitidy(Nm^2)
rho=1.225; %Air Density (Kgm^-3)
M_theta=-1.2; %Non-Dimensional pitch damping derivative
a_w=2*pi; %Lift Curve slove
U_sim=120; %Specific velocity (m/s)
%Inertia, Structural, Aerodynamic Damping , Aerodynamic Stiffness matrices
A = m*[((s^5)*c)/5 (s^4/4)*((c^2/2)-(c*x_f));
(s^4/4)*((c^2/2)-(c*x_f)) (s^3/3)*(c^3/3)-c^2*x_f+c*x_f^2];%Inertria metrix of system
E = [4*EI*s 0;
0 GJ*s];%Structural Matrix
B = [(c*a_w*s^5)/10 0;
(-c^2*e*a_w*s^4)/8 (-c^3*s^3*M_theta)/24];%Aerodynamic damping matrix
C = [0 (c*s^4*a_w);
0 (-e*c^2*s^3*a_w)/6];% Aerodynamic Stiffness matrix
D = [0 0;
0 0];%Structural Damping matrix
A_inv = inv(A);
N = 2;
ZN = zeros(N);
IN = eye(N);
%Frequency Domain
V_air = linspace(0,140,1e3);
wn_i_all = zeros(4,length(V_air));
zeta_i_all = zeros(4,length(V_air));
for i = 1:length(V_air)
V_air_i = V_air(i);
C_t = rho*V_air_i.*B + D;
K_t = rho*V_air_i^2.*C + E;
% AA = [ ZN IN ;
%-A_inv*K_t -A_inv*C_t ];
[eig_v,eig_D] = eig(calc_AA_mat)
d_eig_D_i = diag(eig_D);
d_eig_D_i = sort( d_eig_D_i );
wn_i = sqrt( real( d_eig_D_i ).^2 + imag( d_eig_D_i ).^2 );
zet_i = -real( d_eig_D_i ) ./ wn_i;
wn_i_all(:, i) = wn_i;
zeta_i_all(:,i)=zet_i;
end
subplot (2,1,1)
plot(V_air,wn_i_all(:,:))
xlabel( 'Air speed (m/s)')
ylabel ('Natural Frequency (Hz)')
subplot(2,1,2)
plot(V_air,zeta_i_all(:,:))
xlabel ('Air speed(m/s)')
ylabel ('Damping ratio(%)')
% callling ODE45 Solver
t_range = linspace(0,40,1e3);
Y0 = [1 0 0 0].';
[t,x] = ode45('LAMFWM',t_range,Y0);
subplot(2,1,1)
plot(t,x(:,1))
xlabel('time')
ylabel('displacement')
subplot(2,1,2)
plot(t,x(:,2))
xlabel('time')
ylabel('velocity')
function xdot=LAMFWM(t,x)
global U_sim AA
% v=10;
% xdot_1=x(2);
% xdot_2= (-A_inv)*(rho*v^2*C+E)*x(2)-A_inv*(rho*v*B+D)*x(1)
% xdot=[xdot_1;xdot_2];
AA = calc_AA_mat( U_sim );
xdot = AA * x
end
function AA=calc_AA_mat(U_sim)
global ZN IN A_inv K_t C_t
AA = [ ZN IN ;
-A_inv*K_t -A_inv*C_t ];
end
0 Comments
Answers (1)
Harshendra Shah
on 8 Apr 2020
Edited: Harshendra Shah
on 8 Apr 2020
Hi Marios,
There are multiple errors in your script.
First, you are using C_t and K_t variables inside the function calc_AA_mat. These variables are not defined globally in your script, therefore when you are trying to use these variables inside the functions, their value is empty. Hence, it is throwing error stating "Incorrect dimensions for matrix multiplication". So to resolve this error, declare these variables as global at the top of the script like the other global variables.
Second, you are using LAMFWM function in the ode45 as function handle. You are not supposed to put that in quotes. You should use @functionName syntax there.
[t,x] = ode45(@LAMFWM,t_range,Y0);
You can refer to the following link for the same:
Also, use clc and clear all to clear the MATLAB Command Window and all the variables in the workspace.
I hope this helps.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!