MATLAB Solving 2nd Order ODE with MATLAB:Input Argument "a" is Undefined

AI Thread Summary
The discussion revolves around troubleshooting a MATLAB script for solving a second-order ordinary differential equation (ODE) using the ode45 function. The user encounters an error indicating that the input argument "a" is undefined, specifically at the line where da_dt(1) is assigned the value a(2). Suggestions include renaming the variable "a" to avoid conflicts with MATLAB's predefined variables and ensuring that the function is called correctly. The user also faces issues with the nargin function, which is supposed to set default values for parameters but results in a "too many input arguments" error. It is noted that the function works when parameters are hardcoded instead of passed as arguments. The conversation highlights the importance of variable naming conventions and proper function calling in MATLAB to avoid common errors.
mohamed_ali
Messages
6
Reaction score
0
hi
i am trying to solve second order ODE in MATLAB but when i write my scribt and call into the command window using ode45 i get input argument "a" is undefined i have spent so much time trying to figure out what the problem is but unsuccessful.

the function is as follows

function da_dt=canti(t,a,flag,zeta,epsi,lampda,z,omega)
%this function solves the non-linear equation of motion for a cantilever
%beam with lumped mass




L=125.4; % Lenght of the beam, the units are in mm

r=125.4; % this is the radius of gyration of the beam (mm)

J=600.9; % polar moment of inertia, unit which are mm^4

d=93.68; %position of mass (mm)

E=0.20936e-6; % stiffness of the beam (N/mm^2)

w=11.11; % this is the width of the beam (mm)

m=14.70; % the mass (g)



g=9.81; % acceleration due to gravity (m/s^2)

%lampda=1; lampda=r/L

h=0.368; %the thickness of the beam (mm)

I=4.626e-2; %cross-sectional moment of inertia (mm^4)

rho=0.03177; % the density (g/mm^3)

k=1.609; %radius of curevature of the beam at s





H11=0.977127;
H12=0.977127;
H13=0.687553;
H21=0.977127;
H31=0.977127;
H32=0.168365;
H33=0.153578;
H34=0.270738;
H41=0.325800;
H42=0.578800;
H43=-0.319906;
H51=0.217654;
H52=0.732989;
H53=0.472729;
H61=0.217654;
H62=0.231875;
H63=0.732989;
H64=0.393123;
H65=0.236364;

if nargin<1, zeta=1.469e-3; %this is the damping ratio
end
if nargin<2, epsi=1; %this value is intoduced to show the smallness of damping, nonlinear terms,and excitation
end
if nargin<3, lampda=1;
end

if nargin<4, z=2;% this is the amplitude of the excitation
end

if nargin<5, omega=5; %frequency of excitation
end
mu=m/rho*L;
Q=(H11+mu*H12+J(1)*H13*lampda*lampda);

c=abs(2*zeta*sqrt(E*m));
zeta1=(c*H21)/(2*epsi*rho*Q);

alpha1=((E*I*lampda*lampda)/(epsi*rho*Q*L^4))*((k^4)*(H41+mu*H42)+H43);

J(1)=J/rho*L*r^2;

theta=abs(sqrt(((E*I*k^4)*(H31+mu*H32)/(rho*L^4)-(g/L)*(H33+mu*H34))/Q));

zeta=zeta1/theta;
alpha=alpha1/theta;
phi=omega/theta;

lampda=r/L;

f=(omega*omega*(z))*(H33+mu*H34)/(epsi*(theta^2)*(Q));


K1=((lampda*lampda)*(H51+mu*H52+H53*J(1)*lampda*lampda)/epsi*Q);

K2=((lampda*lampda)*(H61-H62+mu*H63-mu*H64+J(1)*H65*lampda*lampda)/epsi*Q);

da_dt=zeros(2,1);


da_dt(1)=a(2);
da_dt(2)=-((2*zeta*epsi*a(2)+(1 - epsi*f*cos(phi*t))*a(1)+epsi*alpha*(a(1)*a(1)*a(1))+epsi*K1*a(1)*a(2)*a(2))/(1 + epsi*K2*a(1)*a(1)));

end

please can anyone help

thanks
 
Physics news on Phys.org
Does it tell you what line it breaks at? My guess would be to rename the variable "a" to something else.
 
thanks for the reply gb7nash yeah it tells me that error is at da_dt=a(2) but i will try to rename the variable "a" to something else
 
If memory serves me right, you want to avoid using single-letter variables (in any programming language). Sometimes they're predefined to do something else. I can't remember if that occurs on MATLAB and I can't compile it (my license expired). That would be my guess though, the syntax looks fine.
 
How are you calling the function with ode45?

Also, for readability post the code in
Code:
tags...
 
thanks for the reply jhae2.718
i am calling the ode like [t,a]=ode45('canti',[0 20],[0.1;0],[],zeta,epsi,lampda,z,omega)

sorry i didnt understand what you meant by "post the code in
Code:
tags..." and btw i am not using pi (π) am using ϕ (phi)
 
Can you post the exact error? Also, does the function work if you call it directly?

Regarding the code tags, edit your post and type
Code:
 before your code, and
after.
 
mohamed_ali said:
hi
Code:
da_dt(2)=-((2*zeta*epsi*a(2)+(1 - epsi*f*cos(phi*t))*a(1)+epsi*alpha*(a(1)*a(1)*a(1))+epsi*K1*a(1)*a(2)*a(2))/(1 + epsi*K2*a(1)*a(1)));

I'm assuming you want this to be a singular value? As it currently is, it will return a vector the length of t, which will throw a vertcat error when you try and assign it to da_dt.
 
I have noticed the error only occures when I debug the code but when I call the function it seems to be working fine. however another problem i have is that when I give a default value to variables by using nargin< , I get the message "too many input arguments" . by using nargin the value given to the variables should be recognised by the Matlab but its not seem to be doing that instead, i have to write the default values again in the command window.

Code:
function da_dt=canti(t,a,zeta,epsi,lampda,z,omega)
%this function solves the non-linear equation of motion for a cantilever
%beam with lumped mass




L=125.4;   % Lenght of the beam, the units are in mm

r=125.4; % this is the radius of gyration of the beam (mm)

J=600.9; % polar moment of inertia, unit which are mm^4

d=93.68;  %position of mass (mm)

E=0.20936e-6; % stiffness of the beam (N/mm^2)
  
w=11.11;      % this is the width of the beam (mm)

m=14.70;        % the mass (g)



g=9.81; % acceleration due to gravity (m/s^2)

%lampda=1;  lampda=r/L

h=0.368;  %the thickness of the beam (mm)

I=4.626e-2; %cross-sectional moment of inertia (mm^4)

rho=0.03177;  % the density (g/mm^3)

k=1.609;    %radius of curevature of the beam at s





H11=0.977127;
H12=0.977127;
H13=0.687553;
H21=0.977127;
H31=0.977127;
H32=0.168365;
H33=0.153578;
H34=0.270738;
H41=0.325800;
H42=0.578800;
H43=-0.319906;
H51=0.217654;
H52=0.732989;
H53=0.472729;
H61=0.217654;
H62=0.231875;
H63=0.732989;
H64=0.393123;
H65=0.236364;

if nargin<1, zeta=1.469e-3; %this  is the damping ratio
end
if nargin<2, epsi=1;   %this value is intoduced to show the smallness of damping, nonlinear terms,and excitation
end
if nargin<3,  lampda=1;
end

if nargin<4, z=2;%  this is the amplitude of the excitation
end

if nargin<5, omega=5;  %frequency of excitation
end
mu=m/rho*L;
Q=(H11+mu*H12+J(1)*H13*lampda*lampda);

c=abs(2*zeta*sqrt(E*m));
zeta1=(c*H21)/(2*epsi*rho*Q);

alpha1=((E*I*lampda*lampda)/(epsi*rho*Q*L^4))*((k^4)*(H41+mu*H42)+H43);

J(1)=J/rho*L*r^2;

theta=abs(sqrt(((E*I*k^4)*(H31+mu*H32)/(rho*L^4)-(g/L)*(H33+mu*H34))/Q));

zeta=zeta1/theta;
alpha=alpha1/theta;
phi=omega/theta;

lampda=r/L;

f=(omega*omega*(z))*(H33+mu*H34)/(epsi*(theta^2)*(Q));


K1=((lampda*lampda)*(H51+mu*H52+H53*J(1)*lampda*lampda)/epsi*Q);

K2=((lampda*lampda)*(H61-H62+mu*H63-mu*H64+J(1)*H65*lampda*lampda)/epsi*Q);

da_dt=zeros(2,1);


da_dt(1)=a(2);  
da_dt(2)=-((2*zeta*epsi*a(2)+(1 - epsi*f*cos(phi*t))*a(1)+epsi*alpha*(a(1)*a(1)*a(1))+epsi*K1*a(1)*a(2)*a(2))/(1 + epsi*K2*a(1)*a(1)));

the code used to call the function is

Code:
>> zeta=1.469e-3;
>> epsi=1;
>> lampda=1;
>> z=2;
>> omega=5;
>> [t,a]=ode45('canti',[0 72],[0.1;0],[],zeta,epsi,lampda,z,omega);
>> plot(t,a(:,1)); grid on;
 
  • #10
jhae2.718 said:
I'm assuming you want this to be a singular value? As it currently is, it will return a vector the length of t, which will throw a vertcat error when you try and assign it to da_dt.

sorry i don't understand what you mean with vertcat error
 
  • #11
Your code works for me if you just set the values for zeta, epsi ,lampda ,z, and omega in the script, and not use them as function arguments. You could also throw them onto the end of a and set those parameters in the initial a matrix.

Never mind the bit about vertcat.
 
  • #12
yeah it works fine thanks a lot for your help
 
  • #13
No problemo.
 
Back
Top