Problems using ode23 and function arguments in Matlab for differential equations - matlab
I am having problems with the following code. Next is shortened, but it tries to represent a chain of four pendulums.
% Simulation of double pendulum by Lagrangian mechanics
% see also http://www.eng.auburn.edu/~marghitu/MECH6710/06_Fall/MATLAB/C6_analytical/C6_MATLAB.pdf
close all
% generalized coordinates==================================================
syms t real
theta=sym('theta(t)');
phi=sym('phi(t)');
alpha=sym('alpha(t)'); %tercer angulo
gamma=sym('gamma(t)'); %cuarto ángulo
betha=sym('betha(t)'); %quinto ángulo
% constants, length, mass, g===============================================
L1=7; %lengths of all the pendulums
L2=6;
L3=6;
L4=6;
L5=6;
m1=3; %mass of all the particles
m2=3;
m3=3;
m4=3;
m5=3;
g=9.81;
i=0;
% positions and velocities as function of the generalized coordinates======
x1=L1*sin(theta);
y1=-i-L1*cos(theta);
x2=x1+L2*sin(phi);
y2=y1-L2*cos(phi);
x3=x2+L3*sin(alpha);
y3=y2-L3*cos(alpha);
x4=x3+L4*sin(gamma);
y4=y3-L4*cos(gamma);
x5=x4+L5*cos(betha);
y5=y4-L5*sin(betha);
x1dot=diff(x1,t);
x2dot=diff(x2,t);
x3dot=diff(x3,t);
x4dot=diff(x4,t);
x5dot=diff(x5,t);
y1dot=diff(y1,t);
y2dot=diff(y2,t);
y3dot=diff(y3,t);
y4dot=diff(y4,t);
y5dot=diff(y5,t);
% kinetic and potential energy=============================================
T=m1/2*(x1dot^2+y1dot^2)+m2/2*(x2dot^2+y2dot^2)+m3/2*(x3dot^2+y3dot^2)+m4/2*(x4dot^2+y4dot^2)+m5/2*(x5dot^2+y5dot^2);
V=m1*g*y1+m2*g*y2+m3*g*y3+m4*g*y4+m5*g*y5;
% Lagrangian ==============================================================
L=T-V;
% dL/d(qdot)===============================================================
dummy=sym('dummy');
dLthetadot = subs(diff(subs(L,diff(theta,t),dummy),dummy),dummy,diff(theta,t));
dLphidot = subs(diff(subs(L,diff(phi,t),dummy),dummy),dummy,diff(phi,t));
dLalphadot= subs(diff(subs(L,diff(alpha,t),dummy),dummy),dummy,diff(alpha,t));
dLgammadot=subs(diff(subs(L,diff(gamma,t),dummy),dummy),dummy,diff(gamma,t));
dLbetadot=subs(diff(subs(L,diff(betha,t),dummy),dummy),dummy,diff(betha,t));
% dL/dq====================================================================
dLdtheta=subs(diff(subs(L,theta,dummy),dummy),dummy,theta);
dLdphi=subs(diff(subs(L,phi,dummy),dummy),dummy,phi);
dLdalpha=subs(diff(subs(L,alpha,dummy),dummy),dummy,alpha);
dLdgamma=subs(diff(subs(L,gamma,dummy),dummy),dummy,gamma);
dLdbeta=subs(diff(subs(L,betha,dummy),dummy),dummy,betha);
% dFdq=====================================================================
k=0.5; % dissipation constant
F=1/2*k*(x1dot^2+y1dot^2+x2dot^2+y2dot^2+x3dot^2+y3dot^2+x4dot^2+y4dot^2+x5dot^2+y5dot^2);
dFdthetadot=subs(diff(subs(F,diff(theta,t),dummy),dummy),dummy,diff(theta,t));
dFdphidot=subs(diff(subs(F,diff(phi,t),dummy),dummy),dummy,diff(phi,t));
dFdalphadot=subs(diff(subs(F,diff(alpha,t),dummy),dummy),dummy,diff(alpha,t));
dFdgammadot=subs(diff(subs(F,diff(gamma,t),dummy),dummy),dummy,diff(gamma,t));
dFdbetadot=subs(diff(subs(F,diff(betha,t),dummy),dummy),dummy,diff(betha,t));
% generalized equations of motion==========================================
differentialequation1=diff(dLthetadot,t)-dLdtheta+dFdthetadot;
differentialequation2=diff(dLphidot,t)-dLdphi+dFdphidot;
differentialequation3=diff(dLalphadot,t)-dLdalpha+dFdalphadot;
differentialequation4=diff(dLgammadot,t)-dLdgamma+dFdgammadot;
differentialequation5=diff(dLbetadot,t)-dLdbeta+dFdbetadot;
% abbreviation of variables================================================
variables={theta,phi,alpha,gamma,betha,diff(theta,t),diff(phi,t),diff(alpha,t),diff(gamma,t),diff(betha,t),diff(theta,t,2),diff(phi,t,2),diff(alpha,t,2),diff(gamma,t,2),diff(betha,t,2)};
variablesshort={'x(1)','x(2)','x(3)','x(4)','x(5)','x(6)','x(7)','x(8)','x(9)','x(10)','thetaddot','phiddot','alphaddot','gammaddot','betaddot'};
DifferentialEquation1=subs(differentialequation1,variables,variablesshort);
DifferentialEquation2=subs(differentialequation2,variables,variablesshort);
DifferentialEquation3=subs(differentialequation3,variables,variablesshort);
DifferentialEquation4=subs(differentialequation4,variables,variablesshort);
DifferentialEquation5=subs(differentialequation5,variables,variablesshort);
% solve for thetaddot, phiddot=============================================
solution=solve(DifferentialEquation1,DifferentialEquation2,DifferentialEquation3,DifferentialEquation4,DifferentialEquation5,'thetaddot','phiddot','alphaddot','gammaddot','betaddot');
THETADDOT=solution.thetaddot;
PHIDDOT=solution.phiddot;
ALPHADDOT=solution.alphaddot;
GAMMADDOT=solution.gammaddot;
BETADDOT=solution.betaddot;
% solve non linear ode system==============================================
time=linspace(0,30,2000);
% initial conditions [theta,phi,thetadot,phidot]===========================
x0=[pi/4 0 0 0 0 0.5 0 0 0 0];
str=['xdot=#(t,x)[;x(6);x(7);x(8);x(9);x(10);',char(THETADDOT),';',char(PHIDDOT),';',char(ALPHADDOT),';',char(GAMMADDOT),';',char(BETADDOT),'];'];
eval(str);
[t,q]=ode23(xdot,time,x0);
% Calculute positions as function of generalized coordinates===============
X1=L1*sin(q(:,1));
Y1=-L1*cos(q(:,1));
X2=X1+L2*sin(q(:,2));
Y2=Y1-L2*cos(q(:,2));
X3=X2+L3*sin(q(:,3));
Y3=Y2-L3*cos(q(:,3));
X4=X3+L4*sin(q(:,4));
Y4=Y3-L4*cos(q(:,4));
X5=X4+L5*sin(q(:,5));
Y5=Y4-L5*cos(q(:,5));
% plot solution============================================================
set(gcf,'color',[1 1 1])
hold on
box on
axis equal
for i=1:numel(time)
cla;
plot([0,X1(i)],[0,Y1(i)],'k','linewidth',4);
plot(X1(i),Y1(i),'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',4*m1);
plot([X1(i),X2(i)],[Y1(i),Y2(i)],'k','linewidth',4);
plot(X2(i),Y2(i),'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',4*m2);
plot([X2(i),X3(i)],[Y2(i),Y3(i)],'k','linewidth',4);
plot(X3(i),Y3(i),'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',4*m3);
plot([X3(i),X4(i)],[Y3(i),Y4(i)],'k','linewidth',4);
plot(X4(i),Y4(i),'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',4*m4);
plot([X4(i),X5(i)],[Y4(i),Y5(i)],'k','linewidth',4);
plot(X5(i),Y5(i),'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',4*m5);
% plot(X2(1:i),Y2(1:i),'g-'); %aqui pinta la trayectoria de la bola 2
axis([-8,8,-1.05*(L1+L2+L3+L4+L5),0]);
drawnow
end
The error I see is next:
Undefined function or variable 'matrix'. Error in QuadruplePendulum2D>#(t,x)[x(5);x(6);x(7);x(8);matrix(0,1,[]);matrix(0,1,[]);matrix(0,1,[]);matrix(0,1,[])]
Error in odearguments (line 87) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0
Error in odearguments (line 87) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode23 (line 114) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in QuadruplePendulum2D (line 86) [t,q]=ode23(xdot,time,x0);
Can anyone help in this error, please? Thanks
Related
Error while solving State space equations using ode45
My code should output me a column vector of X at the end of the program.I'm getting a lot of errors. Please help! clc; clear; t0=0; tend=.001; T=[t0 .00005]; T1=[.00005 tend]; temp=1; X(:,1) = [0;0;0;0]; for k=1:2 for i= temp:50*(k) [T,X]=ode45(#(T,X)sys(T,X,A1,B1),T,X(:,1)); else [T1,X1]=ode45(#(T1,X1)sys1(T1,X1,A0,B0),T1,X(:,end)); end end temp=50; end function Xdot = sys(T,X,A1,B1,U) Xdot= A1*X + B1*U; end function Xdot = sys1(T1,X,A0,B0,U) Xdot= A0*X + B0*U; end The errors are as follows: Not enough input arguments. Error in odearguments (line 90) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0. Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
It looks like you are not setting the argument U to the functions sys and sys1. I have highlighted the corresponding places below with <U missing>: if sw(i)==0 && X(2,i)> vdon || sw(i)==1 && X(1,i)>0 sw(i+1)=1; [T,X] = ode45( #(T,X) sys(T,X,A1,B1, <U missing> ), T, X(:,1) ); else sw(i+1)=0; [T1,X1] = ode45( #(T1,X1) sys1(T1,X1,A0,B0, <U missing> ), T1, X(:,end) ); end
Trying to use pdepe function to solve spherical diffusion equation
I need to solve the spherical pde diffusion equation: dq/dt=(1/r^2)*d/dr(r^2*D*(dq/dr)) D is a constant initial condition is q=0 at t=0 boundary conditions are: dq/dr=0 at r=0, q=1 at r=Rc I have written the following code but I am getting an error: function pde1 m=2; x=linspace(0.01,1,20); t=linspace(0.01,2,10); sol=pdepe(m,#pdefun,#pdeic,#pdebc,x,t); u=sol(:,:,1); % A surface plot surf(x,t,u) title('Surface plot') xlabel('Distance x') ylabel('Time t') end %----------------------------------- function [c,f,s]=pdefun(x,t,u,DuDx) global d c=1/d; f=DuDx; s=0; end %----------------------------------------------------- function [pl,ql,pr,qr]= pdebc(xl,ul,xr,ur,t) global r pl=0; ql=0; pr=1; qr=r; end %------------------------------------------------ function u0 = pdeic(x) u0=0; end could you please tell me what's wrong with it? The error is: Error using / Matrix dimensions must agree. Error in pdefun (line 3) c=1/d; Error in pdepe (line 246) [c,f,s] = feval(pde,xi(1),t(1),U,Ux,varargin{:}); Error in pde1 (line 5) sol=pdepe(m,#pdefun,#pdeic,#pdebc,x,t); Thanks
Convert 1x1 sym to float
I attempt to convert a 1x1 sym to float in the program below below but it fails. Why? The culprit line: sol = ode45(M,[30 45],[double(H0) double(V0)]); The error message: Error using odearguments (line 111) Inputs must be floats, namely single or double. Error in ode45 (line 114) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ... Error in extra_credit_HW5_ME70 (line 60) sol = ode45(M,[30 45],[subs(H0) subs(V0)]); The code: clc; clear all; close all; %This program solves the rocket program by using a system of second order %differential which I derived (see Homework 5) %code explanation from http://www.mathworks.com/products/symbolic/code-examples.html? file=/products/demos/symbolictlbx/second_order_differential_equation/solve-a-second-order-differential-equation.html Ve=3500; Mo=400; Me=5; g=9.81; Cd=.5; Af=.2; R=287 T=273.15; Patm=101325; syms y(t); %Note: From the hw: "Take the density of air at sea level to be 1.21 %kg/m3." I decided to model the density with an exponential function p(h)=(Patm*exp(-g*y/(R*T))/(R*T)) V = odeToVectorField(diff(y, 2) == Ve*Me/(Mo-Me.*t)-g-.5*Cd* (Patm*exp(-g*y/(R*T))/(R*T)) *diff(y).^2*Af/(Mo-Me.*t)); M = matlabFunction(V,'vars', {'t','Y'}); sol = ode45(M,[0 30],[0 0]); x = linspace(0,30,250); y = deval(sol,x,1); plot(x,y); % legend('No air resistance, air resistance'); hold on; Mo=350; syms y(t); %Initial conditions for case where mass is dumped. H0=y(end); V0=( (y(end)-y(end-1)) / (x(end)-x(end-1)) ); %Note: From the hw: "Take the density of air at sea level to be 1.21 %kg/m3." I decided to model the density with an exponential function p(h)=(Patm*exp(-g*y/(R*T))/(R*T)) V = odeToVectorField(diff(y, 2) == Ve*Me/(Mo-Me.*t)-g-.5*Cd* (Patm*exp(-g*y/(R*T))/(R*T)) *diff(y).^2*Af/(Mo-Me.*t)); M = matlabFunction(V,'vars', {'t','Y'}); sol = ode45(M,[30 45],[double(H0) double(V0)]); x = linspace(30,45,500); y = deval(sol,x,1); plot(x,y,'cyan'); % legend('No air resistance, air resistance'); title('Rocket height versus time for stages launch') xlabel('time (seconds)') ylabel('height (meters)')
Plotting onto specified axes in Matlab GUIDE
I have the following code for a pushbutton using Matlab GUIDE.It is supposed to plot a rotating arrow which rotates to the angles specified by theta 1 and theta 2. function start_pushbutton_callback_Callback(hObject, eventdata, handles) % hObject handle to start_pushbutton_callback (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles = guidata(hObject); theta1 = handles.xy_angle; theta2 = handles.yz_angle; delay = handles.speed; axes(handles.axes_turntable_xy); R= 1; %Radius of the turntable E=[0;0;0]; C=[-2;0;0];% points on the X axis D=[2;0;0]; % points on the X axis G=[0;2;0]; % points on the Y axis H=[0;-2;0];% points on the Y axis for th =1:1:theta1 clf; Radius =1; [x,y,z] = cylinder(Radius,200); plot(x(1,:),y(1,:)) axis equal L1= [R*cosd(th);R*sind(th);0]; drawvector(E,L1); % call the drawvector function, as previously line([C(1),D(1)],[C(2),D(2)]); line([G(1),H(1)],[G(2),H(2)]); axis([-2 2 -2 2]); grid on; pause(delay); end; axes(handles.axes_turntable_yz) ; R= 1; %Radius of the turntable E=[0;0;0]; C=[-2;0;0];% points on the X axis D=[2;0;0]; % points on the X axis G=[0;2;0]; % points on the Y axis H=[0;-2;0];% points on the Y axis for th =1:1:theta2; clf; Radius = 1; [x,y,z] = cylinder(Radius,200); plot(x(1,:),y(1,:)) axis equal L1= [R*cosd(th);R*sind(th);0]; drawvector(E,L1); % call the drawvector function line([C(1),D(1)],[C(2),D(2)]); line([G(1),H(1)],[G(2),H(2)]); axis([-2 2 -2 2]); grid on; pause(delay); end However, I am facing two problems with this code: 1) It simply plots onto a new matlab figure instead of plotting it on axes_turntable_xy- 2) It displays the following error afer executing till the line axes(handles.axes_turntable_yz). The error is as follows: Error using axes Invalid object handle Error in turntable_interface_model>start_pushbutton_callback_Callback (line 235) axes(handles.axes_turntable_yz) ; Error in gui_mainfcn (line 96) feval(varargin{:}); Error in turntable_interface_model (line 42) gui_mainfcn(gui_State, varargin{:}); Error in #(hObject,eventdata)turntable_interface_model('start_pushbutton_callback_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating uicontrol Callback Can anyone help with this?
Okay, I debugged the code and this was the problematic line in the above code: for th =1:1:theta1 clf; Radius =1; [x,y,z] = cylinder(Radius,200); The working code is: for th1 =1:1:theta1 cla(handles.axes_turntable_xy); %clears the figure handles.axes_turntable_xy Radius =1; [x,y,z] = cylinder(Radius,200); This clears the figure I am plotting in in Matlab, not some unspecified figure! Although it's still hard to understand why Matlab was plotting these commands in a new figure inspite of making axes.turntable_xy the current axes.
Second Order Diff Eq with ode45 in Matlab
So I need to solve x''(t) = -x(t)^p with initial conditions x(0)= 0 and v(0) = x'(0) = v_o = 1. The value of the parameter p is 1. This is what I have: function [t, velocity, x] = ode_oscilation(p) y=[0;0;0]; % transform system to the canonical form function y = oscilation_equation(x,p) y=zeros(2,1); y(1)=y(2); y(2)=-(x)^p; % to make matlab happy we need to return a column vector % so we transpose (note the dot in .') y=y.'; end tspan=[0, 30]; % time interval of interest [t,velocity,x] = ode45(#oscilation_equation, tspan, 1); t = y(:,1); xposition=y(:,3); velocity=y(:,2); end and this is the error message I receive: ode_oscillation(1) Error using odearguments (line 91) ODE_OSCILLATION/OSCILATION_EQUATION must return a column vector. Error in ode45 (line 114) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ... Error in ode_oscillation (line 17) [t,velocity,x] = ode45(#oscilation_equation, tspan,1);
There's a few things going wrong here. First, from help ode45: ode45 Solve non-stiff differential equations, medium order method. [TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates the system of differential equations y' = f(t,y) from time T0 to TFINAL with initial conditions Y0. Note that ode45 expects a function f(t,y), where size(t) == [1 1] for time and size(y) == [1 N] or [N 1] for solution values. Your oscilation_equation has the order of input arguments inverted, and you input a constant parameter p instead of time t. Also, the initial conditions Y0 should have the same size as y; so size(y0) == [N 1] or [1 N]. You just have 1, which is clearly causing errors. Also, your output arguments t, xposition and velocity will be completely ignored and erroneous, since y is not set as output argument from ode45, and most of all, their names do not correspond to ode_oscilation's output arguments. Also, their order of extracting from columns of y is incorrect. So, in summary, change everything to this: function [t, v, x] = ode_oscilation(p) % initial values y0 = [0 1]; % time interval of interest tspan =[0 30]; % solve system [t,y] = ode45(#(t,y) [y(2); -y(1)^p], tspan, y0); % and return values of interest x = y(:,1); v = y(:,2); end