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

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting an error encountered while solving a second-order ordinary differential equation (ODE) in MATLAB using the ode45 function. Participants explore issues related to undefined input arguments and variable naming conventions within the function.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports an error indicating that the input argument "a" is undefined when calling the function with ode45.
  • Another participant suggests renaming the variable "a" to avoid potential conflicts with predefined variables in MATLAB.
  • A participant points out that the error occurs specifically at the line where da_dt(1) is assigned the value of a(2).
  • There is a discussion about the use of single-letter variables and their potential to cause issues in programming.
  • Participants inquire about how the function is called with ode45 and suggest posting the code in a specific format for clarity.
  • One participant mentions that the function seems to work fine when called directly, but issues arise when using nargin to set default values for parameters.
  • Another participant suggests that the function could work better if the parameters were set directly in the script rather than as function arguments.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to resolve the error, with multiple suggestions and viewpoints presented regarding variable naming and function calling methods.

Contextual Notes

There are unresolved issues regarding the handling of input arguments and the behavior of the function when called with different parameters. Some participants express confusion over specific error messages and the implications of using nargin.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 46 ·
2
Replies
46
Views
9K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 9 ·
Replies
9
Views
3K