PDA

View Full Version : matlab scribt


mohamed_ali
Apr18-11, 04:29 PM
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*la mpda)/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

gb7nash
Apr18-11, 04:47 PM
Does it tell you what line it breaks at? My guess would be to rename the variable "a" to something else.

mohamed_ali
Apr18-11, 06:45 PM
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

gb7nash
Apr18-11, 06:48 PM
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.

jhae2.718
Apr18-11, 10:07 PM
How are you calling the function with ode45?

Also, for readability post the code in [code][/code] tags...

mohamed_ali
Apr19-11, 08:33 AM
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 tags..." and btw i am not using pi (π) am using ϕ (phi)

jhae2.718
Apr19-11, 08:54 AM
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 [/code] after.

jhae2.718
Apr19-11, 09:32 AM
hi

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.

mohamed_ali
Apr19-11, 09:35 AM
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.


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*la mpda)/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


>> 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;

mohamed_ali
Apr19-11, 09:42 AM
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 dont understand what you mean with vertcat error

jhae2.718
Apr19-11, 09:49 AM
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.

mohamed_ali
Apr19-11, 10:37 AM
yeah it works fine thanks a lot for your help

jhae2.718
Apr19-11, 10:41 AM
No problemo.