├── README.md ├── [Francesco_predictive control(bookos-z1.org).pdf ├── [Grune_L.,_Pannek_J.]_Nonlinear_model_predictive_c(bookos-z1.org).pdf ├── desaraju2016.pdf ├── exp-driven-pred-control-timevarying-NM.pdf ├── experience-driven-predictive-control-NM.pdf ├── fast nmpc UAV.pdf ├── fast-stochastic-MPC-francesco-ucb.pdf ├── fast_mpc_boyd.pdf ├── fast_nmpc ├── actual_traj.mat ├── augPendDynamics.m ├── computeActualCost.m ├── costFunction.m ├── desired.mat ├── dynamics_ode.m ├── generateTraj.m ├── hermiteSimpsonDefects.m ├── init_waypoints.m ├── linDynamics.m ├── nominal.mat ├── nonlincst.m ├── pend_animation.m ├── realPendDynamics.m ├── real_pendulum.m~ ├── results ├── setParams.m ├── simplePendDynamics.m ├── simulated_traj.mat ├── slqMPC_algo2.m ├── slq_algo1.asv ├── slq_algo1.m ├── slq_algo1.m~ └── waypointCost.m ├── fastmpcparticleenumeration.pdf ├── myAutomaticDifferentiation ├── @myA2D │ ├── README.txt │ ├── abs.m │ ├── acos.m │ ├── asin.m │ ├── atan.m │ ├── cos.m │ ├── cumprod.m │ ├── cumsum.m │ ├── diff.m │ ├── display.m │ ├── end.m │ ├── eq.m │ ├── exp.m │ ├── ge.m │ ├── get.m │ ├── getderivs.m │ ├── getsecderiv.m │ ├── getvalue.m │ ├── gt.m │ ├── isnan.m │ ├── le.m │ ├── length.m │ ├── log.m │ ├── lt.m │ ├── max.m │ ├── min.m │ ├── minus.m │ ├── mpower.m │ ├── mtimes.m │ ├── myA2D.m │ ├── ne.m │ ├── norm.m │ ├── plus.m │ ├── power.m │ ├── private │ │ ├── derXder.m │ │ ├── valX2der.m │ │ └── valXder.m │ ├── rdivide.m │ ├── set.m │ ├── sin.m │ ├── size.m │ ├── sort.m │ ├── sqrt.m │ ├── subsasgn.m │ ├── subsref.m │ ├── sum.m │ ├── tan.m │ ├── tanh.m │ ├── times.m │ ├── uminus.m │ ├── uplus.m │ ├── vertcat.m │ └── zeros.m ├── @myAD │ ├── README.txt │ ├── abs.m │ ├── acos.m │ ├── asin.m │ ├── atan.m │ ├── cos.m │ ├── cumprod.m │ ├── cumsum.m │ ├── diff.m │ ├── display.m │ ├── end.m │ ├── eq.m │ ├── exp.m │ ├── ge.m │ ├── get.m │ ├── getderivs.m │ ├── getvalue.m │ ├── gt.m │ ├── isnan.m │ ├── le.m │ ├── length.m │ ├── log.m │ ├── lt.m │ ├── max.m │ ├── min.m │ ├── minus.m │ ├── mpower.m │ ├── mtimes.m │ ├── myAD.m │ ├── ne.m │ ├── norm.m │ ├── plus.m │ ├── power.m │ ├── private │ │ ├── valXder.cpp │ │ ├── valXder.dll │ │ ├── valXder.m │ │ └── valXder.mexw32 │ ├── rdivide.m │ ├── set.m │ ├── sin.m │ ├── size.m │ ├── sort.m │ ├── sqrt.m │ ├── subsasgn.m │ ├── subsref.m │ ├── sum.m │ ├── tan.m │ ├── tanh.m │ ├── times.m │ ├── uminus.m │ ├── uplus.m │ ├── vertcat.m │ └── zeros.m ├── @myMatrixAD │ ├── README.txt │ ├── abs.m │ ├── acos.m │ ├── asin.m │ ├── atan.m │ ├── cos.m │ ├── ctranspose.m │ ├── diag.m │ ├── diff.m │ ├── display.m │ ├── end.m │ ├── eq.m │ ├── exp.m │ ├── ge.m │ ├── get.m │ ├── getderivs.m │ ├── getvalue.m │ ├── gt.m │ ├── horzcat.m │ ├── isnan.m │ ├── le.m │ ├── length.m │ ├── log.m │ ├── lt.m │ ├── max.m │ ├── min.m │ ├── minus.m │ ├── mpower.m │ ├── mtimes.m │ ├── myMatrixAD.m │ ├── ne.m │ ├── norm.m │ ├── plus.m │ ├── power.m │ ├── private │ │ ├── valXder.cpp │ │ ├── valXder.dll │ │ ├── valXder.mexw32 │ │ └── valXder_old.m │ ├── rdivide.m │ ├── set.m │ ├── sin.m │ ├── size.m │ ├── sort.m │ ├── sqrt.m │ ├── subsasgn.m │ ├── subsref.m │ ├── sum.m │ ├── tan.m │ ├── tanh.m │ ├── times.m │ ├── transpose.m │ ├── uminus.m │ ├── uplus.m │ ├── vertcat.m │ └── zeros.m ├── README.txt └── example_AD.m ├── real-time-mpc-steering-ucb.pdf └── trajectory.mat /README.md: -------------------------------------------------------------------------------- 1 | # onlineMPC 2 | 3 | Implementation of the paper "Fast nonlinear Model Predictive Control for unified trajectory optimization and tracking " 4 | 5 | Everything in Matlab. 6 | 7 | Psuedocode followed from the paper. 8 | -------------------------------------------------------------------------------- /[Francesco_predictive control(bookos-z1.org).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/[Francesco_predictive control(bookos-z1.org).pdf -------------------------------------------------------------------------------- /[Grune_L.,_Pannek_J.]_Nonlinear_model_predictive_c(bookos-z1.org).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/[Grune_L.,_Pannek_J.]_Nonlinear_model_predictive_c(bookos-z1.org).pdf -------------------------------------------------------------------------------- /desaraju2016.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/desaraju2016.pdf -------------------------------------------------------------------------------- /exp-driven-pred-control-timevarying-NM.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/exp-driven-pred-control-timevarying-NM.pdf -------------------------------------------------------------------------------- /experience-driven-predictive-control-NM.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/experience-driven-predictive-control-NM.pdf -------------------------------------------------------------------------------- /fast nmpc UAV.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/fast nmpc UAV.pdf -------------------------------------------------------------------------------- /fast-stochastic-MPC-francesco-ucb.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/fast-stochastic-MPC-francesco-ucb.pdf -------------------------------------------------------------------------------- /fast_mpc_boyd.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/fast_mpc_boyd.pdf -------------------------------------------------------------------------------- /fast_nmpc/actual_traj.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/fast_nmpc/actual_traj.mat -------------------------------------------------------------------------------- /fast_nmpc/augPendDynamics.m: -------------------------------------------------------------------------------- 1 | function [xdot, xNext]=augPendDynamics(x,u,modelParams) 2 | xdot(1,:)=x(2,:); 3 | xdot(2,:)=-(modelParams.g/modelParams.length)*sin(x(1,:))+... 4 | u/(modelParams.m*modelParams.length^2)-(((x(1,:).^2)-1).*x(2,:)); 5 | xNext=x+xdot*modelParams.dt; 6 | end 7 | -------------------------------------------------------------------------------- /fast_nmpc/computeActualCost.m: -------------------------------------------------------------------------------- 1 | function J=computeActualCost(actual_traj,desired_traj,modelParams) 2 | wp=init_waypoints(modelParams); 3 | x_diff=actual_traj.x-desired_traj.x; 4 | x_diff(1,:)=wrapToPi(x_diff(1,:)); 5 | u_diff=actual_traj.u-desired_traj.u; 6 | 7 | J=x_diff(:,end)'*modelParams.Qf*x_diff(:,end); 8 | for inst_iter=1:modelParams.N-1 9 | J=J+x_diff(:,inst_iter)'*modelParams.Qt*x_diff(:, inst_iter)... 10 | +(u_diff(inst_iter)-u_diff(inst_iter+1))'*modelParams.Rt*(u_diff(inst_iter)-u_diff(inst_iter+1)); 11 | if modelParams.wp_bool 12 | J=J+waypointCost(actual_traj.x(:,inst_iter),inst_iter*modelParams.dt,wp,modelParams); 13 | end 14 | end 15 | end -------------------------------------------------------------------------------- /fast_nmpc/costFunction.m: -------------------------------------------------------------------------------- 1 | %% params nomTraj=[u;x] %3xN 2 | %% params modelParams 3 | %% return J scalar cost function 4 | function J=costFunction(nomTraj, modelParams) 5 | %used in generateTraj.m 6 | 7 | %%only instantaneous term 8 | J=0; 9 | for iter=1:modelParams.N-1 10 | J=J+nomTraj(2:3,iter)'*modelParams.Qt*nomTraj(2:3,iter)... 11 | +nomTraj(1,iter)'*modelParams.Rt*nomTraj(1,iter); 12 | end 13 | 14 | %final cost 15 | % J=J+nomTraj(2:3,modelParams.N)'*modelParams.Qf*nomTraj(2:3,modelParams.N); 16 | 17 | end -------------------------------------------------------------------------------- /fast_nmpc/desired.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/fast_nmpc/desired.mat -------------------------------------------------------------------------------- /fast_nmpc/dynamics_ode.m: -------------------------------------------------------------------------------- 1 | function xdot=dynamics(t,x,xO,uO,xD,uD,timestamp,K1,K2,tS1,tS2) 2 | xOInterp(1)=interp1(timestamp',xO(:,1),t); 3 | xOInterp(2)=interp1(timestamp',xO(:,2),t); 4 | xDInterp(1)=interp1(timestamp',xD(:,1),t); 5 | xDInterp(2)=interp1(timestamp',xD(:,2),t); 6 | uOInterp=interp1(timestamp',uO,t); 7 | uDInterp=interp1(timestamp',uD,t); 8 | K1Interp(1)=interp1(tS2,K1(:,1),t); 9 | K1Interp(2)=interp1(tS2,K1(:,2),t); 10 | K2Interp=interp1(tS1,K2',t); 11 | xBar=x-xOInterp'; 12 | uDBar=uDInterp-uOInterp; 13 | u_=uDInterp-K1Interp*xBar-K2Interp; 14 | xdot=[x(2); u_ - sin(x(1))-0.3*x(2)]; 15 | end -------------------------------------------------------------------------------- /fast_nmpc/generateTraj.m: -------------------------------------------------------------------------------- 1 | function [x,u]=generateTraj(modelParams, dynamics, x_goal) 2 | %% direct collocation for simple and augmented pendulum 3 | %initialize 4 | initial_traj=[zeros(1,modelParams.N); zeros(2, modelParams.N)]; 5 | 6 | loss=@(initial_traj)costFunction(initial_traj,modelParams); 7 | 8 | A=[]; 9 | b=[]; 10 | Aeq=[]; 11 | beq=[]; 12 | lb=[repmat(-1*modelParams.u_lim, 1, modelParams.N) ;-inf(2, modelParams.N)]; 13 | ub=[repmat(1*modelParams.u_lim, 1, modelParams.N) ;inf(2, modelParams.N)]; 14 | 15 | nonlin_constraints=@(initial_traj)nonlincst(initial_traj,dynamics, x_goal,modelParams); 16 | 17 | options =optimoptions(@fmincon,'TolFun', 0.00000001,'MaxIter', 10000, ... 18 | 'MaxFunEvals', 100000,'Display','iter', ... 19 | 'DiffMinChange', 0.001,'Algorithm', 'sqp'); 20 | 21 | [nom_traj,fval,ef,op]=fmincon(loss, initial_traj, A,b,Aeq,beq,... 22 | lb,ub,nonlin_constraints,options); 23 | 24 | x=nom_traj(2:3,:); 25 | u=nom_traj(1,:); 26 | end -------------------------------------------------------------------------------- /fast_nmpc/hermiteSimpsonDefects.m: -------------------------------------------------------------------------------- 1 | function delta=hermiteSimpsonDefects(xNext,u, dynamics,modelParams) 2 | xLow = xNext(:,1:(end-1)); 3 | xHi = xNext(:,2:end); 4 | [xdot,~] = dynamics(xNext,u); %no time dependence 5 | xdotLow = xdot(:,1:(end-1)); 6 | xdotHi = xdot(:,2:end); 7 | uLow=u(:,1:end-1); 8 | uHi=u(:,2:end); 9 | xck = 0.5*(xLow + xHi)+ (modelParams.dt/8).*(xdotLow-xdotHi); 10 | uck=0.5*(uLow+uHi); 11 | % xdotck=[xck(2,:);-sin(xck(1,:))+uck-(((xck(1,:).^2)-1).*xck(2,:))]; 12 | [xdotck,~]=dynamics(xck,uck); 13 | delta = xLow-xHi + (modelParams.dt/6)*(xdotLow + 4*xdotck + xdotHi); 14 | end -------------------------------------------------------------------------------- /fast_nmpc/init_waypoints.m: -------------------------------------------------------------------------------- 1 | function wp=init_waypoints(modelParams) 2 | %output wp - a vector of structures 3 | for wp_iter=1:modelParams.num_wp 4 | x.state = modelParams.states(:,wp_iter); 5 | x.rho_p = modelParams.rho_p(wp_iter); 6 | x.t_p = modelParams.t_p(wp_iter); 7 | x.weight_p = modelParams.weight_p; 8 | wp{wp_iter} = x; 9 | end 10 | end -------------------------------------------------------------------------------- /fast_nmpc/linDynamics.m: -------------------------------------------------------------------------------- 1 | %% linearization of system dynamics at trajectory points 2 | %returns A 2x2xN B 2xN 3 | function [A,B]=linDynamics(modelParams,trajectory,dis_or_conti) 4 | delta=0.01; 5 | % calculate A 6 | for traj_iter=1:1:size(trajectory.x(1,:),2) 7 | [f_x1_plus,~]=simplePendDynamics(trajectory.x(:,traj_iter)+[delta;0],... 8 | trajectory.u(:,traj_iter),modelParams); 9 | [f_x1_min,~]= simplePendDynamics(trajectory.x(:,traj_iter)-[delta;0],... 10 | trajectory.u(:,traj_iter), modelParams); 11 | [f_x2_plus,~]=simplePendDynamics(trajectory.x(:,traj_iter)+[0;delta],... 12 | trajectory.u(:,traj_iter),modelParams); 13 | [f_x2_min,~]= simplePendDynamics(trajectory.x(:,traj_iter)-[0;delta],... 14 | trajectory.u(:,traj_iter), modelParams); 15 | [f_u_plus,~]=simplePendDynamics(trajectory.x(:,traj_iter),... 16 | trajectory.u(:,traj_iter)+delta,modelParams); 17 | [f_u_min,~]= simplePendDynamics(trajectory.x(:,traj_iter),... 18 | trajectory.u(:,traj_iter)-delta, modelParams); 19 | B(:,traj_iter)=(f_u_plus-f_u_min)/(2*delta); 20 | A(:,:,traj_iter)=[(f_x1_plus-f_x1_min)/(2*delta) ... 21 | (f_x2_plus-f_x2_min)/(2*delta)]; 22 | 23 | %% Discretization- hack from wikipedia 24 | if nargin>2 25 | if dis_or_conti=='discrete' 26 | M=[A(:,:,traj_iter) B(:,traj_iter);... 27 | zeros(size(B(:,traj_iter),2),size(A,1)+size(B(:,traj_iter),2))]*modelParams.dt; 28 | M=expm(M); 29 | A(:,:,traj_iter)=M(1:size(A,1),1:size(A,2)); 30 | B(:,traj_iter)=M(1:size(A,1),end); 31 | end 32 | end 33 | 34 | end 35 | end -------------------------------------------------------------------------------- /fast_nmpc/nominal.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/fast_nmpc/nominal.mat -------------------------------------------------------------------------------- /fast_nmpc/nonlincst.m: -------------------------------------------------------------------------------- 1 | function [c,ceq]=nonlincst(nom_traj,dynamics,x_goal,modelParams) 2 | xNext=nom_traj(2:3,:); 3 | u=nom_traj(1,:); 4 | BoundaryFinal=x_goal-xNext(:,end); 5 | BoundaryInit=modelParams.x_init-xNext(:,1); 6 | delta=hermiteSimpsonDefects(xNext,u,dynamics,modelParams); 7 | c = []; %No inequality constraints 8 | ceq = [BoundaryInit; reshape(delta,2*modelParams.N-2,1); BoundaryFinal; u(end)]; 9 | end -------------------------------------------------------------------------------- /fast_nmpc/pend_animation.m: -------------------------------------------------------------------------------- 1 | function pend_animation(x,fig_pend) 2 | fig_pend; 3 | hold on; 4 | modelParams=setParams(); 5 | O=[0 0]; 6 | axis(gca,'equal'); 7 | axis([-modelParams.length*1.3 modelParams.length*1.3 -modelParams.length*1.3 modelParams.length*1.3]); 8 | grid on; 9 | 10 | % Loop for animation 11 | 12 | for i=1:length(x) 13 | %Mass Point 14 | P=modelParams.length*[sin(x(i)) -cos(x(i))]; 15 | 16 | %circle in origin 17 | O_circ=viscircles(O,0.01,'color','black'); 18 | 19 | % pendulum 20 | pend=line([O(1) P(1)],[O(2) P(2)],'LineWidth',5); 21 | 22 | %ball 23 | ball=viscircles(P,0.03); 24 | 25 | drawnow 26 | 27 | if imodelParams.u_lim 18 | actTraj.u(fwd_iter)=sign(actTraj.u(fwd_iter))*modelParams.u_lim; 19 | end 20 | end 21 | actTraj.u(runtime+1)=actTraj.u(runtime); 22 | 23 | % fig_pend=figure('Name','slq'); 24 | % pend_animation(act_traj.x(1,:),fig_pend); 25 | end -------------------------------------------------------------------------------- /fast_nmpc/real_pendulum.m~: -------------------------------------------------------------------------------- 1 | function real_pendulum() 2 | % changing dynamics 3 | modelParams.m=modelParams.m+0.2; 4 | end -------------------------------------------------------------------------------- /fast_nmpc/results: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/fast_nmpc/results -------------------------------------------------------------------------------- /fast_nmpc/setParams.m: -------------------------------------------------------------------------------- 1 | function modelParams=setParams() 2 | %% algo 1 3 | 4 | modelParams.g=9.8;%9.8 5 | modelParams.m=0.5; 6 | modelParams.length=0.4; 7 | modelParams.c=1; 8 | 9 | modelParams.dt=0.05; 10 | modelParams.T=5; %N=T/dt 11 | modelParams.N=modelParams.T/modelParams.dt+1; 12 | 13 | modelParams.Qt=diag([1,1]); 14 | modelParams.Qf=diag([10,10]); 15 | modelParams.Rt=1; 16 | 17 | modelParams.x_init=[0;0]; 18 | modelParams.u_lim=2.5; 19 | 20 | modelParams.gen_traj=1; 21 | modelParams.viz=1; 22 | modelParams.printf=1; 23 | modelParams.ls_steps=10; 24 | modelParams.alpha_d=1.1; 25 | 26 | modelParams.traj_track=0; % 0 if goal tracking 27 | 28 | %% algo 2 29 | 30 | modelParams.policy_lag=0; 31 | modelParams.Q_lqr=diag([10,10]); 32 | modelParams.mpc_steps=10; 33 | modelParams.goal=[pi;0]; 34 | 35 | % waypoints params 36 | modelParams.wp_bool=0; 37 | modelParams.num_wp=1; 38 | modelParams.states = [pi/2 ;... 39 | 0 ]; 40 | modelParams.rho_p = [100]; 41 | modelParams.t_p = [50*modelParams.dt]; 42 | modelParams.weight_p = diag([1000,0]); 43 | end -------------------------------------------------------------------------------- /fast_nmpc/simplePendDynamics.m: -------------------------------------------------------------------------------- 1 | %% dynamics of a simple pendulum 2 | % params x: state vector 2x1 3 | % params u: input vector 1 4 | % params modelParams: struct 5 | %returns xNext: 2x1, xdot=2x1 6 | function [x_dot, x_next]=simplePendDynamics(x,u, modelParams) 7 | x_dot(1,:)=x(2,:); 8 | x_dot(2,:)=-(modelParams.g/modelParams.length)*sin(x(1,:))-((modelParams.c/modelParams.m)*x(2,:)) +(u/(modelParams.m*modelParams.length^2)); 9 | x_next=x+x_dot*modelParams.dt; 10 | end -------------------------------------------------------------------------------- /fast_nmpc/simulated_traj.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/fast_nmpc/simulated_traj.mat -------------------------------------------------------------------------------- /fast_nmpc/slqMPC_algo2.m: -------------------------------------------------------------------------------- 1 | function slqMPC_algo2 2 | %% writing a good code 3 | % all functions, structs and classes- Camel case 4 | % all variables-underscore 5 | close all 6 | modelParams=setParams(); 7 | 8 | %% initialization 9 | actTraj.x=modelParams.x_init; 10 | actTraj.u=0; 11 | u_ff=zeros(1,modelParams.N-1); 12 | u_fb=zeros(modelParams.N-1,2); 13 | mpc_iter=0; 14 | % N_initial=modelParams.N; 15 | % time_elapsed=0; 16 | 17 | %% loop 18 | while norm(actTraj.x(:,end)-modelParams.goal)>1e-2 19 | %% Adjust Setup 20 | mpc_start=tic; 21 | % acquire current state 22 | curr_state=actTraj.x(:,end); 23 | curr_input=actTraj.u(:,end); 24 | modelParams.x_init=curr_state; 25 | 26 | % if time lag is there, predict state by forward 27 | % simulating dynamics with previous controller 28 | 29 | % update time horizon based on distance 30 | % modelParams.T= ceil(round(abs(wrapToPi(modelParams.goal(1)-curr_state(1))))*(modelParams.T/abs(wrapToPi(modelParams.x_init(1)-modelParams.goal(1))))); 31 | % modelParams.N= floor(modelParams.T/modelParams.dt)+1; 32 | 33 | %% initialize SLQ with controller 34 | 35 | % linearize dynamics about curr_state and curr_input 36 | curr_traj.x=curr_state; 37 | curr_traj.u=curr_input; 38 | [A_0,B_0]=linDynamics(modelParams,curr_traj); 39 | 40 | % compute LQR at linearized state 41 | [K_0,~]=lqr(A_0, B_0, modelParams.Q_lqr, modelParams.Rt); 42 | 43 | % initialize SLQ with LQR around current state 44 | x_desired=modelParams.goal; 45 | nom_traj.x(:,1)=curr_state; 46 | nom_traj.u=zeros(1,modelParams.N); 47 | for fwd_iter=1:modelParams.N-1 48 | nom_traj.u(fwd_iter)=-K_0*(nom_traj.x(:,fwd_iter)-x_desired); 49 | [~,nom_traj.x(:,fwd_iter+1)]=simplePendDynamics(nom_traj.x(:,fwd_iter), nom_traj.u(fwd_iter), modelParams); 50 | end 51 | 52 | %% solve using SLQ 53 | 54 | % run SLQ 55 | modelParams.viz=0; 56 | modelParams.printf=0; 57 | slq_time=tic; 58 | [nomTraj, u_ff, u_fb]=slq_algo1(nom_traj,modelParams, u_ff, u_fb); 59 | slq_done=toc(slq_time); 60 | mpc_done=toc(mpc_start); %equivalent to t_lag 61 | t_lag=mpc_done; 62 | time_elapsed=t_lag*2; 63 | fprintf("the time taken by this MPC iteration = %f for time horizon = %d \n",mpc_done, modelParams.T); 64 | 65 | % send control to closed loop controller 66 | actTrajPart=realPendDynamics(ceil(time_elapsed), modelParams.x_init, nomTraj, u_ff, u_fb, modelParams); 67 | actTraj.x=[actTraj.x actTrajPart.x]; 68 | actTraj.u=[actTraj.u actTrajPart.u]; 69 | mpc_iter=mpc_iter+1; 70 | end 71 | mpc_iter 72 | fig_pend=figure('Name','slq'); 73 | pend_animation(actTraj.x(1,:),fig_pend); 74 | figure('Name','states and inputs'); 75 | subplot(2,3,1); 76 | plot([1:length(actTraj.x(1,:))],actTraj.x(1,:),'b','LineWidth',2); 77 | title('position versus time'); 78 | subplot(2,3,2); 79 | plot([1:length(actTraj.x(2,:))],actTraj.x(2,:),'b','LineWidth',2); 80 | title('velocity versus time'); 81 | subplot(2,3,3); 82 | plot([1:length(actTraj.u)],actTraj.u,'r','LineWidth',2); 83 | title('control input versus time'); 84 | subplot(2,3,[4 5 6]); 85 | plot(actTraj.x(1,:),actTraj.x(2,:),'b','LineWidth',2); 86 | 87 | % save('actual_traj.mat','actual_traj'); 88 | end -------------------------------------------------------------------------------- /fast_nmpc/slq_algo1.asv: -------------------------------------------------------------------------------- 1 | function slq_algo1 2 | %% writing a good code 3 | % all functions, structs and classes- Camel case 4 | % all variables-underscore 5 | close all 6 | 7 | %% main 8 | modelParams=setParams(); 9 | last_l=0; 10 | %% handles for dynamics 11 | simple_pend=@(x,u)simplePendDynamics(x,u, modelParams); 12 | aug_pend=@(x,u)augPendDynamics(x,u, modelParams); 13 | 14 | %% generate trajectory 15 | %to get the full trajectory- add x_init in each traj.x 16 | if modelParams.gen_traj 17 | [nom_traj.x, nom_traj.u]=generateTraj(modelParams, simple_pend,[pi;0] ); 18 | save('nominal.mat','nom_traj'); 19 | [des_traj.x,des_traj.u]=generateTraj(modelParams, aug_pend, [3*pi/4;0.2]); 20 | save('desired.mat','des_traj'); 21 | else 22 | load('nominal.mat','nom_traj'); 23 | load('desired.mat','des_traj'); 24 | end 25 | if modelParams.viz 26 | figure(1); 27 | % 28 | subplot(2,2,1); 29 | plot([1:1:modelParams.N],nom_traj.u) 30 | hold on; 31 | plot([1:1:modelParams.N],des_traj.u) 32 | legend('nominal','desired'); 33 | title('control versus time - given') 34 | hold off; 35 | % 36 | subplot(2,2,2); 37 | plot(nom_traj.x(1,:),nom_traj.x(2,:)) 38 | hold on; 39 | plot(des_traj.x(1,:),des_traj.x(2,:)) 40 | legend('nominal','desired'); 41 | title('state space - given') 42 | hold off; 43 | % 44 | subplot(2,2,3); 45 | title('control versus time-actual'); 46 | % 47 | subplot(2,2,4); 48 | title('state space-actual'); 49 | drawnow 50 | end 51 | %% loop 52 | %repeat until max number of iterations or converged (l(t)modelParams.u_lim 117 | act_traj.u(sim_iter)=sign(act_traj.u(sim_iter))*modelParams.u_lim; 118 | end 119 | [~,act_traj.x(:,sim_iter+1)]=simplePendDynamics(act_traj.x(:,sim_iter),... 120 | act_traj.u(sim_iter),modelParams); 121 | end 122 | act_traj.u(modelParams.N)=0; 123 | J_actual=computeActualCost(act_traj,des_traj,modelParams); 124 | if J_actual=J_nom 128 | act_traj.u=nom_traj.u; 129 | break 130 | end 131 | alpha=alpha/modelParams.alpha_d; 132 | end 133 | if modelParams.viz 134 | subplot(2,2,3); 135 | hold on; 136 | plot([1:1:modelParams.N],act_traj.u,'DisplayName','actual_'+string(max_iter)) 137 | legend('show'); 138 | hold off; 139 | subplot(2,2,4); 140 | hold on; 141 | plot(act_traj.x(1,:),act_traj.x(2,:),'DisplayName','actual_'+string(max_iter)) 142 | legend('show'); 143 | hold off; 144 | drawnow 145 | end 146 | nom_traj.u=act_traj.u; 147 | if abs(last_l-norm(l))<1e-5 148 | break 149 | end 150 | last_l=norm(l); 151 | max_iter=max_iter+1; 152 | end 153 | subplot(2,2,2); 154 | hold on; 155 | plot(act_traj.x(1,:),act_traj.x(2,:),'DisplayName','Actual') 156 | legend('show'); 157 | hold off; 158 | end -------------------------------------------------------------------------------- /fast_nmpc/slq_algo1.m: -------------------------------------------------------------------------------- 1 | function [act_traj,u_ff, u_fb]=slq_algo1(nom_traj,modelParams, u_ff_prev, u_fb_prev) 2 | %% writing a good code 3 | % all functions, structs and classes- Camel case 4 | % all variables-underscore 5 | 6 | close all 7 | set(0,'DefaultFigureWindowStyle','docked'); 8 | %% main 9 | if nargin==0 10 | modelParams=setParams(); 11 | else 12 | u_ff=u_ff_prev; 13 | u_fb=u_fb_prev; 14 | end 15 | converged=0; 16 | 17 | %% handles for dynamics 18 | simple_pend=@(x,u)simplePendDynamics(x,u, modelParams); 19 | aug_pend=@(x,u)augPendDynamics(x,u, modelParams); 20 | 21 | %% generate trajectory 22 | %to get the full trajectory- add x_init in each traj.x 23 | if nargin==0 24 | if modelParams.gen_traj 25 | [nom_traj.x, nom_traj.u]=generateTraj(modelParams, simple_pend,[pi;0] ); 26 | save('nominal.mat','nom_traj'); 27 | if modelParams.traj_track 28 | [des_traj.x,des_traj.u]=generateTraj(modelParams, aug_pend, modelParams.goal); 29 | save('desired.mat','des_traj'); 30 | else 31 | des_traj.x=repmat(modelParams.goal,1,modelParams.N); 32 | des_traj.u=zeros(1,modelParams.N); 33 | save('desired.mat','des_traj'); 34 | end 35 | else 36 | load('nominal.mat','nom_traj'); 37 | load('desired.mat','des_traj'); 38 | assert(abs(des_traj.x(1,end)-modelParams.goal(1))<1e-5,'gen traj should be on'); 39 | end 40 | else % MPC stuff 41 | if modelParams.gen_traj 42 | if modelParams.traj_track 43 | [des_traj.x,des_traj.u]=generateTraj(modelParams, aug_pend, modelParams.goal); 44 | save('desired.mat','des_traj'); 45 | else 46 | des_traj.x=repmat(modelParams.goal,1,modelParams.N); 47 | des_traj.u=zeros(1,modelParams.N); 48 | save('desired.mat','des_traj'); 49 | end 50 | else 51 | load('desired.mat','des_traj'); 52 | end 53 | 54 | % concatenating desired traj vector as per time horizon (MPC) 55 | des_traj.x=des_traj.x(:,size(des_traj.x(1,:),2)-modelParams.N+1:end); 56 | des_traj.u=des_traj.u(:,size(des_traj.u(1,:),2)-modelParams.N+1:end); 57 | end 58 | 59 | %% plotting 60 | 61 | 62 | 63 | if modelParams.viz 64 | figure('Name','states and input'); 65 | % 66 | subplot(2,2,1); 67 | plot([1:1:modelParams.N],nom_traj.u,'b','LineWidth',2,'DisplayName','Initial') 68 | hold on; 69 | plot([1:1:modelParams.N],des_traj.u,'r','LineWidth',2,'DisplayName','Desired') 70 | legend('show'); 71 | title('control versus time - given'); 72 | hold off; 73 | % 74 | subplot(2,2,2); 75 | % plot(nom_traj.x(1,:),nom_traj.x(2,:),'b','LineWidth',2,'DisplayName','Initial') 76 | hold on; 77 | if modelParams.traj_track 78 | plot(des_traj.x(1,:),des_traj.x(2,:),'r','LineWidth',2,'DisplayName','Desired'); 79 | else 80 | plot(modelParams.goal(1), modelParams.goal(2),'r*','DisplayName','Desired'); 81 | end 82 | if modelParams.wp_bool 83 | wp=init_waypoints(modelParams); 84 | for wp_iter=1:modelParams.num_wp 85 | hold on; 86 | plot(wp{wp_iter}.state(1),wp{wp_iter}.state(2),'g*'); 87 | end 88 | 89 | end 90 | legend('Desired','Way points'); 91 | title('state space - given') 92 | hold off; 93 | % 94 | subplot(2,2,3); 95 | title('control versus time-actual'); 96 | % 97 | subplot(2,2,4); 98 | title('state space-actual'); 99 | drawnow 100 | end 101 | 102 | %% loop 103 | %repeat until max number of iterations or converged (l(t)modelParams.u_lim 182 | act_traj.u(sim_iter)=sign(act_traj.u(sim_iter))*modelParams.u_lim; 183 | end 184 | [~,act_traj.x(:,sim_iter+1)]=simplePendDynamics(act_traj.x(:,sim_iter),... 185 | act_traj.u(sim_iter),modelParams); 186 | end 187 | act_traj.u(modelParams.N)=0; 188 | J_actual=computeActualCost(act_traj,des_traj,modelParams); 189 | if J_actual=J_nom 198 | act_traj.u=nom_traj.u; 199 | act_traj.x=nom_traj.x; 200 | converged=1; 201 | break 202 | end 203 | alpha=alpha/modelParams.alpha_d; 204 | end 205 | 206 | if modelParams.viz 207 | subplot(2,2,3); 208 | plot([1:1:modelParams.N],act_traj.u,'LineWidth',2,'DisplayName','sim'+string(max_iter)) 209 | legend('show'); 210 | subplot(2,2,4); 211 | plot(act_traj.x(1,:),act_traj.x(2,:),'LineWidth',2,'DisplayName','sim'+string(max_iter)) 212 | legend('show'); 213 | drawnow 214 | end 215 | 216 | if converged 217 | break 218 | end 219 | 220 | nom_traj.u=act_traj.u; 221 | max_iter=max_iter+1; 222 | end 223 | 224 | %% plot and animate the output 225 | save('simulated_traj.mat','act_traj'); 226 | if modelParams.viz 227 | subplot(2,2,2); 228 | hold on; 229 | plot(act_traj.x(1,:),act_traj.x(2,:),'k','LineWidth',2,'DisplayName','Simulated') 230 | legend('show'); 231 | hold off; 232 | subplot(2,2,1); 233 | hold on; 234 | plot([1:1:modelParams.N],act_traj.u,'k','LineWidth',2,'DisplayName','Simulated') 235 | legend('show'); 236 | hold off; 237 | 238 | % figure 2 239 | figure('Name','states versus time'); 240 | subplot(2,1,1); 241 | hold on; 242 | plot([1:1:length(act_traj.x(1,:))],wrapToPi(act_traj.x(1,:)),'k','LineWidth',2); 243 | plot(modelParams.N, modelParams.goal(1),'r*'); 244 | if modelParams.wp_bool 245 | for wp_iter=1:modelParams.num_wp 246 | plot(wp{wp_iter}.t_p*modelParams.T,wp{wp_iter}.state(1),'g*'); 247 | end 248 | end 249 | legend('position','desired','waypoints'); 250 | title('position versus time'); 251 | hold off; 252 | subplot(2,1,2); 253 | hold on; 254 | plot([1:1:length(act_traj.x(2,:))],act_traj.x(2,:),'k','LineWidth',2); 255 | plot(modelParams.N, modelParams.goal(2),'r*'); 256 | if modelParams.wp_bool 257 | for wp_iter=1:modelParams.num_wp 258 | plot(wp{wp_iter}.t_p*modelParams.T,wp{wp_iter}.state(2),'g*'); 259 | end 260 | end 261 | legend('velocity','desired','waypoints'); 262 | title('velocity versus time'); 263 | hold off; 264 | 265 | %figure 3 266 | fig_pend=figure('Name','slq'); 267 | pend_animation(act_traj.x(1,:),fig_pend); 268 | end 269 | end -------------------------------------------------------------------------------- /fast_nmpc/slq_algo1.m~: -------------------------------------------------------------------------------- 1 | function [u_ff, u_fb, act_traj]=slq_algo1(nom_traj,modelParams) 2 | %% writing a good code 3 | % all functions, structs and classes- Camel case 4 | % all variables-underscore 5 | close all 6 | clc 7 | %% main 8 | if nargin==0 9 | modelParams=setParams(); 10 | end 11 | converged=0; 12 | %% handles for dynamics 13 | simple_pend=@(x,u)simplePendDynamics(x,u, modelParams); 14 | aug_pend=@(x,u)augPendDynamics(x,u, modelParams); 15 | 16 | %% generate trajectory 17 | %to get the full trajectory- add x_init in each traj.x 18 | if nargin==0 19 | if modelParams.gen_traj 20 | [nom_traj.x, nom_traj.u]=generateTraj(modelParams, simple_pend,[pi;0] ); 21 | save('nominal.mat','nom_traj'); 22 | if modelParams.traj_track 23 | [des_traj.x,des_traj.u]=generateTraj(modelParams, aug_pend, modelParams.goal); 24 | save('desired.mat','des_traj'); 25 | else 26 | des_traj.x=repmat(modelParams.goal,1,modelParams.N); 27 | des_traj.u=zeros(1,modelParams.N); 28 | save('desired.mat','des_traj'); 29 | end 30 | else 31 | load('nominal.mat','nom_traj'); 32 | load('desired.mat','des_traj'); 33 | assert(abs(des_traj.x(1,end)-modelParams.goal(1))<1e-5,'gen traj should be on'); 34 | end 35 | else % MPC stuff 36 | if modelParams.gen_traj 37 | if modelParams.traj_track 38 | [des_traj.x,des_traj.u]=generateTraj(modelParams, aug_pend, modelParams.goal); 39 | save('desired.mat','des_traj'); 40 | else 41 | des_traj.x=repmat(modelParams.goal,1,modelParams.N); 42 | des_traj.u=zeros(1,modelParams.N); 43 | save('desired.mat','des_traj'); 44 | end 45 | else 46 | load('desired.mat','des_traj'); 47 | end 48 | 49 | % figure(2); 50 | % if modelParams.traj_track 51 | % plot(des_traj.x(1,:),des_traj.x(2,:),'b','LineWidth',2,'DisplayName','Desired'); 52 | % else 53 | % plot(modelParams.goal(1), modelParams.goal(2),'r*'); 54 | % end 55 | 56 | % concatenating desired traj vector as per time horizon (MPC) 57 | des_traj.x=des_traj.x(:,size(des_traj.x(1,:),2)-modelParams.N+1:end); 58 | des_traj.u=des_traj.u(:,size(des_traj.u(1,:),2)-modelParams.N+1:end); 59 | end 60 | 61 | %% plotting 62 | 63 | 64 | 65 | if modelParams.viz 66 | figure('Name','states and input'); 67 | % 68 | subplot(2,2,1); 69 | plot([1:1:modelParams.N],nom_traj.u,'b','LineWidth',2,'DisplayName','Initial') 70 | hold on; 71 | plot([1:1:modelParams.N],des_traj.u,'r','LineWidth',2,'DisplayName','Desired') 72 | legend('show'); 73 | title('control versus time - given'); 74 | hold off; 75 | % 76 | subplot(2,2,2); 77 | % plot(nom_traj.x(1,:),nom_traj.x(2,:),'b','LineWidth',2,'DisplayName','Initial') 78 | hold on; 79 | if modelParams.traj_track 80 | plot(des_traj.x(1,:),des_traj.x(2,:),'r','LineWidth',2,'DisplayName','Desired'); 81 | else 82 | plot(modelParams.goal(1), modelParams.goal(2),'r*','DisplayName','Desired'); 83 | end 84 | if modelParams.wp_bool 85 | wp=init_waypoints(modelParams); 86 | for wp_iter=1:modelParams.num_wp 87 | hold on; 88 | plot(wp{wp_iter}.state(1),wp{wp_iter}.state(2),'g*'); 89 | end 90 | 91 | end 92 | legend('Desired','Way points'); 93 | title('state space - given') 94 | hold off; 95 | % 96 | subplot(2,2,3); 97 | title('control versus time-actual'); 98 | % 99 | subplot(2,2,4); 100 | title('state space-actual'); 101 | drawnow 102 | end 103 | 104 | %% loop 105 | %repeat until max number of iterations or converged (l(t)modelParams.u_lim 183 | act_traj.u(sim_iter)=sign(act_traj.u(sim_iter))*modelParams.u_lim; 184 | end 185 | [~,act_traj.x(:,sim_iter+1)]=simplePendDynamics(act_traj.x(:,sim_iter),... 186 | act_traj.u(sim_iter),modelParams); 187 | end 188 | act_traj.u(modelParams.N)=0; 189 | J_actual=computeActualCost(act_traj,des_traj,modelParams); 190 | if J_actual=J_nom 195 | act_traj.u=nom_traj.u; 196 | act_traj.x=nom_traj.x; 197 | converged=1; 198 | break 199 | end 200 | alpha=alpha/modelParams.alpha_d; 201 | end 202 | 203 | if modelParams.viz 204 | subplot(2,2,3); 205 | plot([1:1:modelParams.N],act_traj.u,'LineWidth',2,'DisplayName','sim'+string(max_iter)) 206 | legend('show'); 207 | subplot(2,2,4); 208 | plot(act_traj.x(1,:),act_traj.x(2,:),'LineWidth',2,'DisplayName','sim'+string(max_iter)) 209 | legend('show'); 210 | drawnow 211 | end 212 | 213 | if converged 214 | u_ff= 215 | break 216 | end 217 | 218 | nom_traj.u=act_traj.u; 219 | max_iter=max_iter+1; 220 | end 221 | 222 | %% plot and animate the output 223 | save('simulated_traj.mat','act_traj'); 224 | if modelParams.viz 225 | subplot(2,2,2); 226 | hold on; 227 | plot(act_traj.x(1,:),act_traj.x(2,:),'k','LineWidth',2,'DisplayName','Simulated') 228 | legend('show'); 229 | hold off; 230 | subplot(2,2,1); 231 | hold on; 232 | plot([1:1:modelParams.N],act_traj.u,'k','LineWidth',2,'DisplayName','Simulated') 233 | legend('show'); 234 | hold off; 235 | 236 | % figure 2 237 | figure('Name','states versus time'); 238 | subplot(2,1,1); 239 | hold on; 240 | plot([1:1:length(act_traj.x(1,:))],wrapToPi(act_traj.x(1,:)),'k','LineWidth',2); 241 | plot(modelParams.N, modelParams.goal(1),'r*'); 242 | if modelParams.wp_bool 243 | for wp_iter=1:modelParams.num_wp 244 | plot(wp{wp_iter}.t_p*modelParams.T,wp{wp_iter}.state(1),'g*'); 245 | end 246 | end 247 | legend('position','desired','waypoints'); 248 | title('position versus time'); 249 | hold off; 250 | subplot(2,1,2); 251 | hold on; 252 | plot([1:1:length(act_traj.x(2,:))],act_traj.x(2,:),'k','LineWidth',2); 253 | plot(modelParams.N, modelParams.goal(2),'r*'); 254 | if modelParams.wp_bool 255 | for wp_iter=1:modelParams.num_wp 256 | plot(wp{wp_iter}.t_p*modelParams.T,wp{wp_iter}.state(2),'g*'); 257 | end 258 | end 259 | legend('velocity','desired','waypoints'); 260 | title('velocity versus time'); 261 | hold off; 262 | 263 | %figure 3 264 | fig_pend=figure('Name','slq'); 265 | pend_animation(act_traj.x(1,:),fig_pend); 266 | end 267 | end -------------------------------------------------------------------------------- /fast_nmpc/waypointCost.m: -------------------------------------------------------------------------------- 1 | function W=waypointCost(x,t,wp,modelParams) 2 | W=0; 3 | for wp_iter=1:modelParams.num_wp 4 | xcap=x-wp{wp_iter}.state; 5 | xcap(1,:)=wrapToPi(xcap(1,:)); 6 | W=W+xcap'*wp{wp_iter}.weight_p*xcap*sqrt(wp{wp_iter}.rho_p/(2*pi))*exp(-wp{wp_iter}.rho_p/2*(t-wp{wp_iter}.t_p)^2); 7 | end 8 | end -------------------------------------------------------------------------------- /fastmpcparticleenumeration.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/fastmpcparticleenumeration.pdf -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/README.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * myAD, myA2D and myMatrixAD - Automatic Differentiation of 1st and 2nd Order 3 | * Copyright (C) 2006 Martin Fink. (martinfink "at" gmx.at) 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2.1 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this library; if not, visit 17 | * http://www.gnu.org/licenses/gpl.html or write to the Free Software 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 19 | */ 20 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/abs.m: -------------------------------------------------------------------------------- 1 | function x = abs(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.secderiv = valX2der(sign(x.values), x.secderiv); 7 | x.derivatives = valXder(sign(x.values), x.derivatives); 8 | x.values = abs(x.values); 9 | 10 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/acos.m: -------------------------------------------------------------------------------- 1 | function x = acos(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | temp = -1./sqrt(1-x.values.^2); 7 | x.secderiv = valX2der(temp.^3.*x.values, derXder(x.derivatives, x.derivatives)) + ... 8 | valX2der(temp, x.secderiv); 9 | x.derivatives = valXder(temp, x.derivatives); 10 | x.values = acos(x.values); 11 | 12 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/asin.m: -------------------------------------------------------------------------------- 1 | function x = asin(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | temp = 1./sqrt(1-x.values.^2); 7 | x.secderiv = valX2der(temp.^3.*x.values, derXder(x.derivatives, x.derivatives)) + ... 8 | valX2der(temp, x.secderiv); 9 | x.derivatives = valXder(temp, x.derivatives); 10 | x.values = asin(x.values); 11 | 12 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/atan.m: -------------------------------------------------------------------------------- 1 | function x = atan(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | temp = 1./(1+x.values.^2); 7 | x.secderiv = valX2der(-temp.^2*2.*x.values, derXder(x.derivatives, x.derivatives)) + ... 8 | valX2der(temp, x.secderiv); 9 | x.derivatives = valXder(temp, x.derivatives); 10 | x.values = atan(x.values); 11 | 12 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/cos.m: -------------------------------------------------------------------------------- 1 | function x = cos(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | temp = sin(x.values); 7 | x.values = cos(x.values); 8 | x.secderiv = valX2der(-x.values, derXder(x.derivatives, x.derivatives)) + ... 9 | valX2der(-temp, x.secderiv); 10 | x.derivatives = valXder(-temp, x.derivatives); 11 | 12 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/cumprod.m: -------------------------------------------------------------------------------- 1 | function x = cumprod(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | n = size(x.values,1); 7 | for i = 2:n 8 | x.secderiv(i,:,:) = squeeze(x.values(i-1)*x.secderiv(i,:,:) + x.values(i)*x.secderiv(i-1,:,:)) + ... 9 | x.derivatives(i-1,:)'*x.derivatives(i,:) + x.derivatives(i,:)'*x.derivatives(i-1,:); 10 | x.derivatives(i,:) = x.values(i-1).*x.derivatives(i,:) + x.values(i).*x.derivatives(i-1,:); 11 | x.values(i) = x.values(i-1).*x.values(i); 12 | end 13 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/cumsum.m: -------------------------------------------------------------------------------- 1 | function x = cumsum(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.secderiv = cumsum(x.secderiv); 7 | x.derivatives = cumsum(x.derivatives); 8 | x.values = cumsum(x.values); 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/diff.m: -------------------------------------------------------------------------------- 1 | function x = diff(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = diff(x.values); 7 | x.derivatives = diff(x.derivatives); 8 | x.secderiv = diff(x.secderiv); 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/display.m: -------------------------------------------------------------------------------- 1 | function display(x, varargin) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if (nargin < 2) 7 | disp([inputname(1) ':']); 8 | else 9 | disp([varargin{1} ':']); 10 | end 11 | disp('Values ='); 12 | disp(x.values); 13 | disp('Derivatives ='); 14 | disp(x.derivatives); 15 | disp('Second Derivatives ='); 16 | disp(x.secderiv); 17 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/end.m: -------------------------------------------------------------------------------- 1 | function idx = end(x, k, n) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if (k==1) 7 | idx = length(x.values); 8 | else 9 | idx = 1; 10 | end 11 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/eq.m: -------------------------------------------------------------------------------- 1 | function z = eq(x, y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | z = x.values == y.values; 9 | else 10 | z = x.values == y; 11 | end 12 | else 13 | z = x == y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/exp.m: -------------------------------------------------------------------------------- 1 | function x = exp(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = exp(x.values); 7 | temp = x.derivatives; 8 | x.derivatives = valXder(x.values, x.derivatives); 9 | x.secderiv = derXder(x.derivatives, temp) + ... 10 | valX2der(x.values, x.secderiv); 11 | 12 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/ge.m: -------------------------------------------------------------------------------- 1 | function z = ge(x, y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | z = x.values >= y.values; 9 | else 10 | z = x.values >= y; 11 | end 12 | else 13 | z = x >= y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/get.m: -------------------------------------------------------------------------------- 1 | function varargout = get(x, varargin) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargin <2 7 | varargout = []; 8 | display(x, inputname(1)); 9 | return; 10 | end 11 | 12 | try 13 | varargout{1} = x.(varargin{1}); 14 | catch 15 | error(sprintf('Field %s doesn''t exist.', varargin{1})); 16 | end 17 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/getderivs.m: -------------------------------------------------------------------------------- 1 | function Jac = getderivs(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | Jac = x.derivatives; 7 | 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/getsecderiv.m: -------------------------------------------------------------------------------- 1 | function secderiv = getsecderiv(mya2d_x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | secderiv = mya2d_x.secderiv; 7 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/getvalue.m: -------------------------------------------------------------------------------- 1 | function val = getvalue(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | val = x.values; 7 | 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/gt.m: -------------------------------------------------------------------------------- 1 | function z = gt(x, y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | z = x.values > y.values; 9 | else 10 | z = x.values > y; 11 | end 12 | else 13 | z = x > y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/isnan.m: -------------------------------------------------------------------------------- 1 | function y = isnan(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | y = isnan(x.values) | isnan(sum(x.derivatives,2)) | isnan(sum(sum(x.secderiv,3),2)); 7 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/le.m: -------------------------------------------------------------------------------- 1 | function z = le(x, y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | z = x.values <= y.values; 9 | else 10 | z = x.values <= y; 11 | end 12 | else 13 | z = x <= y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/length.m: -------------------------------------------------------------------------------- 1 | function mylength = length(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | mylength = length(x.values); 7 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/log.m: -------------------------------------------------------------------------------- 1 | function x = log(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | temp = 1./x.values; 7 | x.secderiv = valX2der(-1./x.values.^2, derXder(x.derivatives, x.derivatives)) + ... 8 | valX2der(temp, x.secderiv); 9 | x.derivatives = valXder(temp, x.derivatives); 10 | x.values = log(x.values); 11 | 12 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/lt.m: -------------------------------------------------------------------------------- 1 | function z = lt(x, y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | z = x.values < y.values; 9 | else 10 | z = x.values < y; 11 | end 12 | else 13 | z = x < y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/max.m: -------------------------------------------------------------------------------- 1 | function z = max(varargin) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargout > 1 7 | error('Only one output for max implemented'); 8 | end 9 | 10 | if nargin > 1 11 | x = max(varargin{1}.values, varargin{2}.values); 12 | idx = (x == varargin{2}.values) + 1; 13 | z = varargin{idx}; 14 | else 15 | [x, idx] = max(varargin{1}.values); 16 | z = varargin{1}; 17 | z.values = z.values(idx); 18 | z.derivatives = z.derivatives(idx,:); 19 | z.secderiv = z.secderiv(idx,:); 20 | end 21 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/min.m: -------------------------------------------------------------------------------- 1 | function z = min(varargin) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargout > 1 7 | error('Only one output for min implemented'); 8 | end 9 | 10 | if nargin > 1 11 | x = min(varargin{1}.values, varargin{2}.values); 12 | idx = (x == varargin{2}.values) + 1; 13 | z = varargin{idx}; 14 | else 15 | [x, idx] = min(varargin{1}.values); 16 | z = varargin{1}; 17 | z.values = z.values(idx); 18 | z.derivatives = z.derivatives(idx,:); 19 | z.secderiv = z.secderiv(idx,:); 20 | end 21 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/minus.m: -------------------------------------------------------------------------------- 1 | function x = minus(x,y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | x.values = x.values - y.values; 9 | x.derivatives = x.derivatives - y.derivatives; 10 | x.secderiv = x.secderiv - y.secderiv; 11 | else 12 | x.values = x.values - y; 13 | end 14 | else 15 | y.values = x - y.values ; 16 | y.derivatives = - y.derivatives; 17 | y.secderiv = - y.secderiv; 18 | x = y; 19 | end 20 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/mpower.m: -------------------------------------------------------------------------------- 1 | function x = mpower(x,y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if (length(x)~=1 & length(y)~=1) 7 | error('Matrix multiplication not implemented, yet. Use ".*" if possible.'); 8 | end 9 | 10 | if isa(x, 'myA2D') 11 | if isa(y, 'myA2D') 12 | tempA = x.values.^(y.values-2); % x^(y-2) 13 | tempB = x.values.^(y.values-1); % x^(y-1) 14 | tempC = x.values.^(y.values); % x^(y) 15 | tempD = log(x.values); % log(x) 16 | x.secderiv = valX2der(tempB.*(1+y.values.*tempD), derXder(x.derivatives, y.derivatives) + derXder(y.derivatives, x.derivatives)) + ... 17 | valX2der(y.values.*(y.values-1).*tempA, derXder(x.derivatives, x.derivatives)) + ... 18 | valX2der(tempC.*tempD.^2, derXder(y.derivatives, y.derivatives)) + ... 19 | valX2der(y.values.*tempB, x.secderiv) + valX2der(tempC.*tempD, y.secderiv); 20 | x.derivatives = valXder(y.values.*tempB, x.derivatives) ... 21 | + valXder(tempC.*tempD, y.derivatives); 22 | x.values = tempC; 23 | else 24 | tempA = x.values.^(y-2); % x^(y-2) 25 | tempB = tempA.*x.values; % x^(y-1) 26 | tempC = tempB.*x.values; % x^(y) 27 | x.secderiv = valX2der(y.*(y-1).*tempA, derXder(x.derivatives, x.derivatives)) + ... 28 | valX2der(y.*tempB, x.secderiv); 29 | x.derivatives = valXder(y.*tempB, x.derivatives); 30 | x.values = tempC; 31 | end 32 | else 33 | y.values = x.^y.values; 34 | temp = log(x); 35 | y.secderiv = valX2der(y.values.*temp.^2, derXder(y.derivatives, y.derivatives)) + ... 36 | valX2der(y.values.*temp, y.secderiv); 37 | y.derivatives = valXder(y.values.*temp, y.derivatives); 38 | x = y; 39 | end 40 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/mtimes.m: -------------------------------------------------------------------------------- 1 | function x = mtimes(x, y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if (length(x)~=1 & length(y)~=1) 7 | error('Matrix times not implemented, yet. Use ".*" if possible.'); 8 | end 9 | 10 | if isa(x, 'myA2D') 11 | if isa(y, 'myA2D') 12 | x.secderiv = valX2der(y.values, x.secderiv) + derXder(x.derivatives, y.derivatives) + ... 13 | derXder(y.derivatives, x.derivatives) + valX2der(x.values, y.secderiv); 14 | x.derivatives = valXder(y.values, x.derivatives) + valXder(x.values, y.derivatives); 15 | x.values = x.values.*y.values; 16 | else 17 | x.secderiv = valX2der(y, x.secderiv); 18 | x.derivatives = valXder(y, x.derivatives); 19 | x.values = x.values.*y; 20 | end 21 | else 22 | y.secderiv = valX2der(x, y.secderiv); 23 | y.derivatives = valXder(x, y.derivatives); 24 | y.values = x.*y.values; 25 | x = y; 26 | end 27 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/myA2D.m: -------------------------------------------------------------------------------- 1 | function x = myA2D(x, varargin) 2 | % /* 3 | % * myAD and myA2D - Automatic Differentiation of 1st and 2nd Derivative 4 | % * Copyright (C) 2006 Martin Fink. (martinfink "at" gmx.at) 5 | % * 6 | % * This library is free software; you can redistribute it and/or 7 | % * modify it under the terms of the GNU General Public License 8 | % * as published by the Free Software Foundation; either version 2.1 9 | % * of the License, or (at your option) any later version. 10 | % * 11 | % * This library is distributed in the hope that it will be useful, 12 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | % * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | % * General Public License for more details. 15 | % * 16 | % * You should have received a copy of the GNU General Public License 17 | % * along with this library; if not, visit 18 | % * http://www.gnu.org/licenses/gpl.html or write to the Free Software 19 | % * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 20 | % */ 21 | 22 | if (isa(x, 'myA2D')) 23 | return; 24 | end 25 | 26 | struct_x.values = x(:); 27 | if (nargin < 3) 28 | n = length(x); 29 | struct_x.derivatives = eye(n); 30 | struct_x.secderiv = zeros(n,n,n); 31 | else 32 | struct_x.derivatives = varargin{1}; 33 | struct_x.secderiv = varargin{2}; 34 | end 35 | x = class(struct_x, 'myA2D'); 36 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/ne.m: -------------------------------------------------------------------------------- 1 | function z = ne(x, y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | z = x.values ~= y.values; 9 | else 10 | z = x.values ~= y; 11 | end 12 | else 13 | z = x ~= y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/norm.m: -------------------------------------------------------------------------------- 1 | function x = norm(x, p) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if (nargin==1) p = 2; end 7 | x = sum(abs(x).^p).^(1/p); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/plus.m: -------------------------------------------------------------------------------- 1 | function x = plus(x, y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | x.values = x.values + y.values; 9 | x.derivatives = x.derivatives + y.derivatives; 10 | x.secderiv = x.secderiv + y.secderiv; 11 | else 12 | x.values = x.values + y; 13 | end 14 | else 15 | y.values = x + y.values; 16 | x = y; 17 | end 18 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/power.m: -------------------------------------------------------------------------------- 1 | function x = power(x,y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | tempA = x.values.^(y.values-2); % x^(y-2) 9 | tempB = x.values.^(y.values-1); % x^(y-1) 10 | tempC = x.values.^(y.values); % x^(y) 11 | tempD = log(x.values); % log(x) 12 | x.secderiv = valX2der(tempB.*(1+y.values.*tempD), derXder(x.derivatives, y.derivatives) + derXder(y.derivatives, x.derivatives)) + ... 13 | valX2der(y.values.*(y.values-1).*tempA, derXder(x.derivatives, x.derivatives)) + ... 14 | valX2der(tempC.*tempD.^2, derXder(y.derivatives, y.derivatives)) + ... 15 | valX2der(y.values.*tempB, x.secderiv) + valX2der(tempC.*tempD, y.secderiv); 16 | x.derivatives = valXder(y.values.*tempB, x.derivatives) ... 17 | + valXder(tempC.*tempD, y.derivatives); 18 | x.values = tempC; 19 | else 20 | tempA = x.values.^(y-2); % x^(y-2) 21 | tempB = tempA.*x.values; % x^(y-1) 22 | tempC = tempB.*x.values; % x^(y) 23 | x.secderiv = valX2der(y.*(y-1).*tempA, derXder(x.derivatives, x.derivatives)) + ... 24 | valX2der(y.*tempB, x.secderiv); 25 | x.derivatives = valXder(y.*tempB, x.derivatives); 26 | x.values = tempC; 27 | end 28 | else 29 | y.values = x.^y.values; 30 | temp = log(x); 31 | y.secderiv = valX2der(y.values.*temp.^2, derXder(y.derivatives, y.derivatives)) + ... 32 | valX2der(y.values.*temp, y.secderiv); 33 | y.derivatives = valXder(y.values.*temp, y.derivatives); 34 | x = y; 35 | end 36 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/private/derXder.m: -------------------------------------------------------------------------------- 1 | function secder = derXder(derA, derB) 2 | 3 | for i = 1:size(derA,1) 4 | secder(i,:,:) = derA(i,:)' * derB(i,:); 5 | end 6 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/private/valX2der.m: -------------------------------------------------------------------------------- 1 | function secder = valX2der(val, secder) 2 | 3 | for i = 1:size(val,1) 4 | secder(i,:,:) = val(i) .* secder(i,:,:); 5 | end 6 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/private/valXder.m: -------------------------------------------------------------------------------- 1 | function der = valXder(val, der) 2 | 3 | der = val(:,ones(size(der,2),1)) .* der; 4 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/rdivide.m: -------------------------------------------------------------------------------- 1 | function x = rdivide(x,y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | temp = valXder(y.values, x.derivatives) - valXder(x.values, y.derivatives); 9 | x.secderiv = valX2der(1./y.values, x.secderiv) ... 10 | + valX2der(-x.values./y.values.^2, y.secderiv) ... 11 | + valX2der(-1./y.values.^3, derXder(temp, y.derivatives) + derXder(y.derivatives, temp)); 12 | x.derivatives = valXder(1./y.values.^2, temp); 13 | x.values = x.values./y.values; 14 | else 15 | x.secderiv = valX2der(1./y, x.secderiv); 16 | x.derivatives = valXder(1./y, x.derivatives); 17 | x.values = x.values./y; 18 | end 19 | else 20 | temp = x./y.values.^2; 21 | y.secderiv = valX2der(-temp, y.secderiv) ... 22 | + valX2der(2.*x./y.values.^3, derXder(y.derivatives, y.derivatives)); 23 | y.derivatives = valXder(-temp, y.derivatives); 24 | y.values = x./y.values; 25 | x = y; 26 | end 27 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/set.m: -------------------------------------------------------------------------------- 1 | function set(x, varargin) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | display(x, inputname(1)); 7 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/sin.m: -------------------------------------------------------------------------------- 1 | function x = sin(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | temp = cos(x.values); 7 | x.values = sin(x.values); 8 | x.secderiv = valX2der(-x.values, derXder(x.derivatives, x.derivatives)) + ... 9 | valX2der(temp, x.secderiv); 10 | x.derivatives = valXder(temp, x.derivatives); 11 | 12 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/size.m: -------------------------------------------------------------------------------- 1 | function varargout = size(x, varargin) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargin == 1 7 | [sx, sy] = size(x.values); 8 | if nargout <= 1 9 | varargout = {[sx, sy]}; 10 | else 11 | varargout = {sx, sy}; 12 | end 13 | else 14 | sx = size(x.values, varargin{:}); 15 | varargout = {sx}; 16 | end 17 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/sort.m: -------------------------------------------------------------------------------- 1 | function varargout = sort(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | [val, idx] = sort(x.values); 7 | x.values = val; 8 | x.derivatives = x.derivatives(idx,:); 9 | x.secderiv = x.secderiv(idx,:,:); 10 | varargout{1} = x; 11 | if (nargout>1) 12 | varargout{2} = idx; 13 | end 14 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/sqrt.m: -------------------------------------------------------------------------------- 1 | function x = sqrt(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = sqrt(x.values); 7 | temp = 1./x.values; 8 | x.secderiv = valX2der(-0.25*temp.^3, derXder(x.derivatives, x.derivatives)) + ... 9 | valX2der(0.5*temp, x.secderiv); 10 | x.derivatives = valXder(0.5*temp, x.derivatives); 11 | 12 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/subsasgn.m: -------------------------------------------------------------------------------- 1 | function y = subsasgn(y, S, x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | if isempty(S.subs{1}) return; end 6 | if (length(S.subs)>1) error('No matrix assignments!'); end 7 | 8 | if isa(x, 'myA2D') 9 | if isa(y, 'myA2D') 10 | y.values(S.subs{:},1) = x.values; 11 | y.derivatives(S.subs{1},:) = x.derivatives; 12 | y.secderiv(S.subs{1},:,:) = x.secderiv; 13 | else 14 | nd = size(x.derivatives,2); 15 | ny = length(y); 16 | testDeriv = zeros(ny, nd); 17 | testSecDeriv = zeros(ny, nd, nd); 18 | y = myA2D(y, testDeriv, testSecDeriv); 19 | y.values(S.subs{:},1) = x.values; 20 | y.derivatives(S.subs{1},:) = x.derivatives; 21 | y.secderiv(S.subs{1},:,:) = x.secderiv; 22 | end 23 | else 24 | y.values(S.subs{:},1) = x; 25 | y.derivatives(S.subs{1},:) = 0; 26 | y.secderiv(S.subs{1},:,:) = 0; 27 | end 28 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/subsref.m: -------------------------------------------------------------------------------- 1 | function x = subsref(x, S) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = x.values(S.subs{:}); 7 | x.derivatives = x.derivatives(S.subs{1},:); 8 | x.secderiv = x.secderiv(S.subs{1},:,:); 9 | 10 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/sum.m: -------------------------------------------------------------------------------- 1 | function x = sum(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = sum(x.values); 7 | x.derivatives = sum(x.derivatives); 8 | x.secderiv = sum(x.secderiv); 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/tan.m: -------------------------------------------------------------------------------- 1 | function x = tan(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | temp = cos(x.values); 7 | x.secderiv = valX2der(2./temp.^3.*sin(x.values), derXder(x.derivatives, x.derivatives)) + ... 8 | valX2der(1./temp.^2, x.secderiv); 9 | x.derivatives = valXder(1./temp.^2, x.derivatives); 10 | x.values = tan(x.values); 11 | 12 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/tanh.m: -------------------------------------------------------------------------------- 1 | function x = tanh(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | temp = 1./(cosh(x.values).^2); 7 | x.secderiv = valX2der(-2*sinh(x.values)./(cosh(x.values).^3), derXder(x.derivatives, x.derivatives)) + ... 8 | valX2der(temp, x.secderiv); 9 | x.derivatives = valXder(temp, x.derivatives); 10 | x.values = tanh(x.values); 11 | 12 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/times.m: -------------------------------------------------------------------------------- 1 | function x = times(x,y) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myA2D') 7 | if isa(y, 'myA2D') 8 | x.secderiv = valX2der(y.values, x.secderiv) + derXder(x.derivatives, y.derivatives) + ... 9 | derXder(y.derivatives, x.derivatives) + valX2der(x.values, y.secderiv); 10 | x.derivatives = valXder(y.values, x.derivatives) + valXder(x.values, y.derivatives); 11 | x.values = x.values.*y.values; 12 | else 13 | x.secderiv = valX2der(y, x.secderiv); 14 | x.derivatives = valXder(y, x.derivatives); 15 | x.values = x.values.*y; 16 | end 17 | else 18 | y.secderiv = valX2der(x, y.secderiv); 19 | y.derivatives = valXder(x, y.derivatives); 20 | y.values = x.*y.values; 21 | x = y; 22 | end 23 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/uminus.m: -------------------------------------------------------------------------------- 1 | function x = uminus(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = - x.values; 7 | x.derivatives = - x.derivatives; 8 | x.secderiv = - x.secderiv; 9 | 10 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/uplus.m: -------------------------------------------------------------------------------- 1 | function x = uplus(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/vertcat.m: -------------------------------------------------------------------------------- 1 | function x = vertcat(varargin) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | y = []; i = 1; 7 | while (~isa(varargin{i}, 'myA2D')) 8 | y = [y; varargin{i}]; 9 | i=i+1; 10 | end 11 | 12 | x = varargin{i}; 13 | if (i>1) 14 | ny = length(y); 15 | nd = size(x.derivatives,2); 16 | x.values = [y; x.values]; 17 | x.derivatives = [zeros(ny, nd); x.derivatives]; 18 | x.secderiv = [zeros(ny, nd, nd); x.secderiv]; 19 | end 20 | 21 | for j = i+1:nargin 22 | if (isa(varargin{j}, 'myA2D')) 23 | x.values = [x.values; varargin{j}.values]; 24 | x.derivatives = [x.derivatives; varargin{j}.derivatives]; 25 | x.secderiv = [x.secderiv; varargin{j}.secderiv]; 26 | else 27 | x.values = [x.values; varargin{j}]; 28 | n = length(varargin{j}); 29 | x.derivatives(end+n,end) = 0; 30 | x.secderiv(end+n,end,end) = 0; 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myA2D/zeros.m: -------------------------------------------------------------------------------- 1 | function x = zeros(x) 2 | % In Package myA2D - Automatic Differentiation of 1st and 2nd Derivative 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = x.values*0; 7 | x.derivatives = x.derivatives*0; 8 | x.secderiv = x.secderiv*0; 9 | 10 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/README.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * myAD, myA2D and myMatrixAD - Automatic Differentiation of 1st and 2nd Order 3 | * Copyright (C) 2006 Martin Fink. (martinfink "at" gmx.at) 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2.1 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this library; if not, visit 17 | * http://www.gnu.org/licenses/gpl.html or write to the Free Software 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 19 | */ 20 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/abs.m: -------------------------------------------------------------------------------- 1 | function x = abs(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | temp = sign(x.values); 7 | x.derivatives = temp(:,ones(size(x.derivatives,2),1)).*x.derivatives; 8 | x.values = abs(x.values); 9 | 10 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/acos.m: -------------------------------------------------------------------------------- 1 | function x = acos(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(-1./sqrt(1-x.values.^2), x.derivatives); 7 | x.values = acos(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/asin.m: -------------------------------------------------------------------------------- 1 | function x = asin(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(1./sqrt(1-x.values.^2), x.derivatives); 7 | x.values = asin(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/atan.m: -------------------------------------------------------------------------------- 1 | function x = atan(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(1./(1+x.values.^2), x.derivatives); 7 | x.values = atan(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/cos.m: -------------------------------------------------------------------------------- 1 | function x = cos(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(-sin(x.values), x.derivatives); 7 | x.values = cos(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/cumprod.m: -------------------------------------------------------------------------------- 1 | function x = cumprod(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | n = size(x.values,1); 7 | for i = 2:n 8 | x.derivatives(i,:) = x.values(i-1).*x.derivatives(i,:) + x.values(i).*x.derivatives(i-1,:); 9 | x.values(i) = x.values(i-1).*x.values(i); 10 | end 11 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/cumsum.m: -------------------------------------------------------------------------------- 1 | function x = cumsum(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = cumsum(x.derivatives); 7 | x.values = cumsum(x.values); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/diff.m: -------------------------------------------------------------------------------- 1 | function x = diff(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = diff(x.values); 7 | x.derivatives = diff(x.derivatives); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/display.m: -------------------------------------------------------------------------------- 1 | function display(x, varargin) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if (nargin < 2) 7 | disp([inputname(1) ':']); 8 | else 9 | disp([varargin{1} ':']); 10 | end 11 | disp('Values ='); 12 | disp(x.values); 13 | disp('Derivatives ='); 14 | disp(x.derivatives); 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/end.m: -------------------------------------------------------------------------------- 1 | function idx = end(x, k, n) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if (k==1) 7 | idx = length(x.values); 8 | else 9 | idx = 1; 10 | end 11 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/eq.m: -------------------------------------------------------------------------------- 1 | function z = eq(x, y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myAD') 7 | if isa(y, 'myAD') 8 | z = x.values == y.values; 9 | else 10 | z = x.values == y; 11 | end 12 | else 13 | z = x == y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/exp.m: -------------------------------------------------------------------------------- 1 | function x = exp(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = exp(x.values); 7 | x.derivatives = valXder(x.values, x.derivatives); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/ge.m: -------------------------------------------------------------------------------- 1 | function z = ge(x, y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myAD') 7 | if isa(y, 'myAD') 8 | z = x.values >= y.values; 9 | else 10 | z = x.values >= y; 11 | end 12 | else 13 | z = x >= y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/get.m: -------------------------------------------------------------------------------- 1 | function varargout = get(x, varargin) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargin <2 7 | varargout = []; 8 | display(x, inputname(1)); 9 | return; 10 | end 11 | 12 | try 13 | varargout{1} = x.(varargin{1}); 14 | catch 15 | error(sprintf('Field %s doesn''t exist.', varargin{1})); 16 | end 17 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/getderivs.m: -------------------------------------------------------------------------------- 1 | function Jac = getderivs(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | Jac = x.derivatives; 7 | 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/getvalue.m: -------------------------------------------------------------------------------- 1 | function val = getvalue(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | val = x.values; 7 | 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/gt.m: -------------------------------------------------------------------------------- 1 | function z = gt(x, y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myAD') 7 | if isa(y, 'myAD') 8 | z = x.values > y.values; 9 | else 10 | z = x.values > y; 11 | end 12 | else 13 | z = x > y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/isnan.m: -------------------------------------------------------------------------------- 1 | function y = isnan(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | y = isnan(x.values) | isnan(sum(x.derivatives,2)); 7 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/le.m: -------------------------------------------------------------------------------- 1 | function z = le(x, y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myAD') 7 | if isa(y, 'myAD') 8 | z = x.values <= y.values; 9 | else 10 | z = x.values <= y; 11 | end 12 | else 13 | z = x <= y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/length.m: -------------------------------------------------------------------------------- 1 | function mylength = length(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | mylength = length(x.values); 7 | 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/log.m: -------------------------------------------------------------------------------- 1 | function x = log(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(1./x.values, x.derivatives); 7 | x.values = log(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/lt.m: -------------------------------------------------------------------------------- 1 | function z = lt(x, y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myAD') 7 | if isa(y, 'myAD') 8 | z = x.values < y.values; 9 | else 10 | z = x.values < y; 11 | end 12 | else 13 | z = x < y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/max.m: -------------------------------------------------------------------------------- 1 | function z = max(varargin) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargout > 1 7 | error('Only one output for max implemented'); 8 | end 9 | 10 | if nargin > 1 11 | x = max(varargin{1}.values, varargin{2}.values); 12 | idx = (x == varargin{2}.values) + 1; 13 | z = varargin{idx}; 14 | else 15 | [x, idx] = max(varargin{1}.values); 16 | z = varargin{1}; 17 | z.values = z.values(idx); 18 | z.derivatives = z.derivatives(idx,:); 19 | end 20 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/min.m: -------------------------------------------------------------------------------- 1 | function z = min(varargin) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargout > 1 7 | error('Only one output for min implemented'); 8 | end 9 | 10 | if nargin > 1 11 | x = min(varargin{1}.values, varargin{2}.values); 12 | idx = (x == varargin{2}.values) + 1; 13 | z = varargin{idx}; 14 | else 15 | [x, idx] = min(varargin{1}.values); 16 | z = varargin{1}; 17 | z.values = z.values(idx); 18 | z.derivatives = z.derivatives(idx,:); 19 | end 20 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/minus.m: -------------------------------------------------------------------------------- 1 | function x = minus(x,y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myAD') 7 | if isa(y, 'myAD') 8 | x.values = x.values - y.values; 9 | x.derivatives = x.derivatives - y.derivatives; 10 | else 11 | x.values = x.values - y; 12 | end 13 | else 14 | y.values = x - y.values ; 15 | y.derivatives = - y.derivatives; 16 | x = y; 17 | end 18 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/mpower.m: -------------------------------------------------------------------------------- 1 | function x = mpower(x,y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if (length(x)~=1 & length(y)~=1) 7 | error('Matrix multiplication not implemented, yet. Use ".*" if possible.'); 8 | end 9 | 10 | if isa(y, 'myAD') 11 | if isa(x, 'myAD') 12 | temp1 = x.values.^(y.values); 13 | temp2 = temp1.*log(x.values); 14 | temp3 = y.values.*x.values.^(y.values-1); 15 | ssOnes = ones(size(x.derivatives,2),1); 16 | x.derivatives = temp3(:,ssOnes).*x.derivatives + temp2(:,ssOnes).*y.derivatives; 17 | x.values = temp1; 18 | else 19 | y.values = x.^y.values; 20 | temp = y.values.*log(x); 21 | y.derivatives = temp(:,ones(size(y.derivatives,1),1)).*y.derivatives; 22 | x = y; 23 | end 24 | else 25 | temp = y.*x.values.^(y-1); 26 | x.derivatives = temp(:,ones(size(x.derivatives,1),1)).*x.derivatives; 27 | x.values = x.values.^y; 28 | end 29 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/mtimes.m: -------------------------------------------------------------------------------- 1 | function x = mtimes(x, y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if (length(x)~=1 & length(y)~=1) 7 | error('Matrix times not implemented, yet. Use ".*" if possible.'); 8 | end 9 | 10 | if isa(x, 'myAD') 11 | if isa(y, 'myAD') 12 | ssOnes = ones(size(x.derivatives,2),1); 13 | x.derivatives = y.values(:,ssOnes).*x.derivatives + x.values(:,ssOnes).*y.derivatives; 14 | x.values = x.values.*y.values; 15 | else 16 | x.values = x.values.*y; 17 | x.derivatives = y(:,ones(size(x.derivatives,2),1)).*x.derivatives; 18 | end 19 | else 20 | y.values = x.*y.values; 21 | y.derivatives = x(:,ones(size(y.derivatives,2),1)).*y.derivatives; 22 | x = y; 23 | end 24 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/myAD.m: -------------------------------------------------------------------------------- 1 | function x = myAD(x, varargin) 2 | % /* 3 | % * myAD and myA2D - Automatic Differentiation of 1st and 2nd Derivative 4 | % * Copyright (C) 2006 Martin Fink. (martinfink "at" gmx.at) 5 | % * 6 | % * This library is free software; you can redistribute it and/or 7 | % * modify it under the terms of the GNU General Public License 8 | % * as published by the Free Software Foundation; either version 2.1 9 | % * of the License, or (at your option) any later version. 10 | % * 11 | % * This library is distributed in the hope that it will be useful, 12 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | % * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | % * General Public License for more details. 15 | % * 16 | % * You should have received a copy of the GNU General Public License 17 | % * along with this library; if not, visit 18 | % * http://www.gnu.org/licenses/gpl.html or write to the Free Software 19 | % * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 20 | % */ 21 | 22 | if (isa(x, 'myAD')) 23 | return; 24 | end 25 | 26 | struct_x.values = x(:); 27 | if (nargin < 2) 28 | struct_x.derivatives = eye(length(x)); 29 | else 30 | struct_x.derivatives = varargin{1}; 31 | end 32 | x = class(struct_x, 'myAD'); 33 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/ne.m: -------------------------------------------------------------------------------- 1 | function z = ne(x, y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myAD') 7 | if isa(y, 'myAD') 8 | z = x.values ~= y.values; 9 | else 10 | z = x.values ~= y; 11 | end 12 | else 13 | z = x ~= y.values; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/norm.m: -------------------------------------------------------------------------------- 1 | function x = norm(x, p) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if (nargin==1) p = 2; end 7 | x = sum(abs(x).^p).^(1/p); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/plus.m: -------------------------------------------------------------------------------- 1 | function x = plus(x, y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myAD') 7 | if isa(y, 'myAD') 8 | x.values = x.values + y.values; 9 | x.derivatives = x.derivatives + y.derivatives; 10 | else 11 | x.values = x.values + y; 12 | end 13 | else 14 | y.values = x + y.values; 15 | x = y; 16 | end 17 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/power.m: -------------------------------------------------------------------------------- 1 | function x = power(x,y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(y, 'myAD') 7 | if isa(x, 'myAD') 8 | temp1 = x.values.^(y.values); 9 | temp2 = temp1.*log(x.values); 10 | temp3 = y.values.*x.values.^(y.values-1); 11 | ssOnes = ones(size(x.derivatives,2),1); 12 | x.derivatives = temp3(:,ssOnes).*x.derivatives + temp2(:,ssOnes).*y.derivatives; 13 | x.values = temp1; 14 | else 15 | y.values = x.^y.values; 16 | temp = y.values.*log(x); 17 | y.derivatives = temp(:,ones(size(y.derivatives,2),1)).*y.derivatives; 18 | x = y; 19 | end 20 | else 21 | temp = y.*x.values.^(y-1); 22 | x.derivatives = temp(:,ones(size(x.derivatives,2),1)).*x.derivatives; 23 | x.values = x.values.^y; 24 | end 25 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/private/valXder.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************\ 2 | * Columnwise array multiplication between Matrix and Vector * 3 | \**************************************************************/ 4 | 5 | #include "mex.h" 6 | #define Out plhs[0] 7 | #define Vec prhs[0] 8 | #define Mat prhs[1] 9 | 10 | void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) 11 | { 12 | if (nlhs > 1) { 13 | mexErrMsgTxt("Too many output arguments."); 14 | } 15 | switch(nrhs) { 16 | default: { 17 | mexErrMsgTxt("Incorrect number of arguments."); 18 | } 19 | case 0: { 20 | mexPrintf("\nColumnwise dot-product matrix with vector.\n\n"); 21 | break; 22 | } 23 | case 2: { 24 | int m=mxGetN(Vec); 25 | int n=mxGetN(Mat); 26 | Out=mxCreateDoubleMatrix(0,0,mxREAL); 27 | if (m == 1 && n > 0) { 28 | int i, j, k=0; 29 | const int* e=mxGetDimensions(Mat); 30 | double* or; 31 | double* inMat=mxGetPr(Mat); 32 | double* inVec=mxGetPr(Vec); 33 | mxSetDimensions(Out,e,2); 34 | mxSetPr(Out,or=(double*) mxMalloc(e[0]*e[1]*sizeof(double))); 35 | for (i=0; i1) 11 | varargout{2} = idx; 12 | end 13 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/sqrt.m: -------------------------------------------------------------------------------- 1 | function x = sqrt(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = sqrt(x.values); 7 | x.derivatives = valXder(0.5./x.values, x.derivatives); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/subsasgn.m: -------------------------------------------------------------------------------- 1 | function y = subsasgn(y, S, x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | if isempty(S.subs{1}) return; end 6 | if (length(S.subs)>1) error('No matrix assignments!'); end 7 | 8 | if isa(x, 'myAD') 9 | if isa(y, 'myAD') 10 | y.values(S.subs{:},1) = x.values; 11 | y.derivatives(S.subs{1},:) = x.derivatives; 12 | else 13 | nd = size(x.derivatives,2); 14 | ny = length(y); 15 | testDeriv = zeros(ny, nd); 16 | y = myAD(y, testDeriv); 17 | y.values(S.subs{:},1) = x.values; 18 | y.derivatives(S.subs{1},:) = x.derivatives; 19 | end 20 | else 21 | y.values(S.subs{:},1) = x; 22 | y.derivatives(S.subs{1},:) = 0; 23 | end 24 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/subsref.m: -------------------------------------------------------------------------------- 1 | function x = subsref(x, S) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = x.values(S.subs{:}); 7 | x.derivatives = x.derivatives(S.subs{1},:); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/sum.m: -------------------------------------------------------------------------------- 1 | function x = sum(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = sum(x.derivatives); 7 | x.values = sum(x.values); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/tan.m: -------------------------------------------------------------------------------- 1 | function x = tan(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(1./cos(x.values).^2, x.derivatives); 7 | x.values = tan(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/tanh.m: -------------------------------------------------------------------------------- 1 | function x = tanh(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(1./(cosh(x.values).^2), x.derivatives); 7 | x.values = tanh(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/times.m: -------------------------------------------------------------------------------- 1 | function x = times(x,y) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myAD') 7 | ssOnes = ones(size(x.derivatives,2),1); 8 | if isa(y, 'myAD') 9 | x.derivatives = y.values(:,ssOnes).*x.derivatives + x.values(:,ssOnes).*y.derivatives; 10 | x.values = x.values.*y.values; 11 | else 12 | x.values = x.values.*y; 13 | x.derivatives = y(:,ssOnes).*x.derivatives; 14 | end 15 | else 16 | y.values = x.*y.values; 17 | y.derivatives = x(:,ones(size(y.derivatives,2),1)).*y.derivatives; 18 | x = y; 19 | end 20 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/uminus.m: -------------------------------------------------------------------------------- 1 | function x = uminus(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = - x.values; 7 | x.derivatives = - x.derivatives; 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/uplus.m: -------------------------------------------------------------------------------- 1 | function x = uplus(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | 6 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/vertcat.m: -------------------------------------------------------------------------------- 1 | function x = vertcat(varargin) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | 6 | y = []; i = 1; 7 | while (~isa(varargin{i}, 'myAD')) 8 | y = [y; varargin{i}]; 9 | i=i+1; 10 | end 11 | 12 | x = varargin{i}; 13 | if (i>1) 14 | n = length(y); 15 | x.values = [y; x.values]; 16 | x.derivatives = [zeros(n, size(x.derivatives,2)); x.derivatives]; 17 | end 18 | 19 | for j = i+1:nargin 20 | if isa(varargin{j}, 'myAD') 21 | x.values = [x.values; varargin{j}.values]; 22 | x.derivatives = [x.derivatives; varargin{j}.derivatives]; 23 | elseif (~isempty(varargin{j})) 24 | x.values = [x.values; varargin{j}]; 25 | x.derivatives(end+length(varargin{j}),end) = 0; 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myAD/zeros.m: -------------------------------------------------------------------------------- 1 | function x = zeros(x) 2 | % In Package myAD - Automatic Differentiation 3 | % by Martin Fink, May 2007 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = x.values*0; 7 | x.derivatives = x.derivatives*0; 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/README.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * myAD, myA2D and myMatrixAD - Automatic Differentiation of 1st and 2nd Order 3 | * Copyright (C) 2006 Martin Fink. (martinfink "at" gmx.at) 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2.1 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this library; if not, visit 17 | * http://www.gnu.org/licenses/gpl.html or write to the Free Software 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 19 | */ 20 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/abs.m: -------------------------------------------------------------------------------- 1 | function x = abs(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(sign(x.values), x.derivatives); 7 | x.values = abs(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/acos.m: -------------------------------------------------------------------------------- 1 | function x = acos(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(-1./sqrt(1-x.values.^2), x.derivatives); 7 | x.values = acos(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/asin.m: -------------------------------------------------------------------------------- 1 | function x = asin(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(1./sqrt(1-x.values.^2), x.derivatives); 7 | x.values = asin(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/atan.m: -------------------------------------------------------------------------------- 1 | function x = atan(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(1./(1+x.values.^2), x.derivatives); 7 | x.values = atan(x.values); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/cos.m: -------------------------------------------------------------------------------- 1 | function x = cos(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(-sin(x.values), x.derivatives); 7 | x.values = cos(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/ctranspose.m: -------------------------------------------------------------------------------- 1 | function x = ctranspose(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = conj(permute(x.values, [2 1])); 7 | x.derivatives = conj(permute(x.derivatives, [2 1 3])); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/diag.m: -------------------------------------------------------------------------------- 1 | function z = diag(x, k) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargin < 2 7 | k = 0; 8 | end 9 | 10 | z = x; 11 | z.values = diag(x.values, k); 12 | sz = size(z.values); 13 | if min(sz) == 1 %% matrix to vector 14 | z.derivatives = zeros(sz(1),sz(2),x.nVariables); 15 | for i = 1:x.nVariables 16 | z.derivatives(:,:,i) = diag(x.derivatives(:,:,i), k); 17 | end 18 | else 19 | z.derivatives = zeros(sz(1),sz(2),x.nVariables); 20 | for i = 1:x.nVariables 21 | z.derivatives(:,:,i) = diag(x.derivatives(:,:,i), k); 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/diff.m: -------------------------------------------------------------------------------- 1 | function x = diff(x, n, dim) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargin < 3 7 | s = size(x.values); 8 | if s(1) == 1 9 | dim = 2; 10 | else 11 | dim = 1; 12 | end 13 | if nargin < 2 14 | n = 1; 15 | end 16 | end 17 | 18 | x.values = diff(x.values,n,dim); 19 | x.derivatives = diff(x.derivatives,n,dim); 20 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/display.m: -------------------------------------------------------------------------------- 1 | function display(x, varargin) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargin < 2 7 | disp([inputname(1) ':']); 8 | else 9 | disp([varargin{1} ':']); 10 | end 11 | disp('Values ='); 12 | disp(x.values); 13 | disp('Derivatives ='); 14 | disp(x.derivatives); 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/end.m: -------------------------------------------------------------------------------- 1 | function idx = end(x, k, n) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if k == 1 && min(size(x.values)) == 1 7 | idx = length(x.values); 8 | return 9 | end 10 | 11 | if k < 3 12 | idx = size(x.values,k); 13 | else 14 | idx = 1; 15 | end 16 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/eq.m: -------------------------------------------------------------------------------- 1 | function z = eq(x, y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(y, 'myMatrixAD') 7 | if isa(x, 'myMatrixAD') 8 | z = x.values == y.values; 9 | else 10 | z = x == y.values; 11 | end 12 | else 13 | z = x.values == y; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/exp.m: -------------------------------------------------------------------------------- 1 | function x = exp(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = exp(x.values); 7 | x.derivatives = valXder(x.values, x.derivatives); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/ge.m: -------------------------------------------------------------------------------- 1 | function z = ge(x, y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(y, 'myMatrixAD') 7 | if isa(x, 'myMatrixAD') 8 | z = x.values >= y.values; 9 | else 10 | z = x >= y.values; 11 | end 12 | else 13 | z = x.values >= y; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/get.m: -------------------------------------------------------------------------------- 1 | function varargout = get(x, varargin) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargin <2 7 | varargout = []; 8 | display(x, inputname(1)); 9 | return; 10 | end 11 | 12 | try 13 | varargout{1} = x.(varargin{1}); 14 | catch 15 | error(sprintf('Field %s doesn''t exist.', varargin{1})); 16 | end 17 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/getderivs.m: -------------------------------------------------------------------------------- 1 | function Jac = getderivs(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | Jac = squeeze(x.derivatives); 7 | if (size(Jac,2)==1) Jac = Jac.'; end 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/getvalue.m: -------------------------------------------------------------------------------- 1 | function val = getvalue(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | val = x.values; 7 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/gt.m: -------------------------------------------------------------------------------- 1 | function z = gt(x, y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(y, 'myMatrixAD') 7 | if isa(x, 'myMatrixAD') 8 | z = x.values > y.values; 9 | else 10 | z = x > y.values; 11 | end 12 | else 13 | z = x.values > y; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/horzcat.m: -------------------------------------------------------------------------------- 1 | function x = horzcat(varargin) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | y = []; i = 1; 7 | while ~isa(varargin{i}, 'myMatrixAD') 8 | y = [y, varargin{i}]; 9 | i=i+1; 10 | end 11 | 12 | x = varargin{i}; 13 | if i>1 14 | x.values = [y, x.values]; 15 | x.derivatives = [zeros([size(y), x.nVariables]), x.derivatives]; 16 | end 17 | 18 | for j = i+1:nargin 19 | if isa(varargin{j}, 'myMatrixAD') 20 | x.values = [x.values, varargin{j}.values]; 21 | x.derivatives = [x.derivatives, varargin{j}.derivatives]; 22 | elseif ~isempty(varargin{j}) 23 | x.values = [x.values, varargin{j}]; 24 | [sx, sy] = size(varargin{j}); 25 | x.derivatives(sx, end+sy, x.nVariables) = 0; 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/isnan.m: -------------------------------------------------------------------------------- 1 | function y = isnan(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | y = isnan(x.values) | isnan(sum(x.derivatives,3)); 7 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/le.m: -------------------------------------------------------------------------------- 1 | function z = le(x, y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(y, 'myMatrixAD') 7 | if isa(x, 'myMatrixAD') 8 | z = x.values <= y.values; 9 | else 10 | z = x <= y.values; 11 | end 12 | else 13 | z = x.values <= y; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/length.m: -------------------------------------------------------------------------------- 1 | function mylength = length(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | mylength = length(x.values); 7 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/log.m: -------------------------------------------------------------------------------- 1 | function x = log(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(1./x.values, x.derivatives); 7 | x.values = log(x.values); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/lt.m: -------------------------------------------------------------------------------- 1 | function z = lt(x, y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(y, 'myMatrixAD') 7 | if isa(x, 'myMatrixAD') 8 | z = x.values < y.values; 9 | else 10 | z = x < y.values; 11 | end 12 | else 13 | z = x.values < y; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/max.m: -------------------------------------------------------------------------------- 1 | function varargout = max(varargin) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | % Many problems with this routine 7 | % max neglects the derivative with respect to the time at the moment 8 | % idx has to be rescaled after the output idx = idx.*diff(t(1:2)); 9 | % 10 | % Overall it's a rough approx at the moment, but better than nought. 11 | 12 | % warning('The max-function should be only used, when the vector corresponds to a linspaced function f(t). Otherwise use a workaround in your code.'); 13 | if nargin == 2 14 | maxx = max(varargin{1}.values, varargin{2}.values); 15 | idx = find(maxx == [varargin{1}.values, varargin{2}.values]); 16 | x = varargin{idx}; 17 | % x.values = maxx; 18 | % x.derivatives = varargin{idx}.derivatives; 19 | if nargout < 2 20 | varargout = {x}; 21 | else 22 | varargout = {x, idx}; 23 | end 24 | return; 25 | else 26 | x = varargin{:}; 27 | end 28 | 29 | if min(size(x)) > 1 30 | error('Matrix maximum not implemented.'); 31 | end 32 | 33 | [maxx, idx] = max(x.values); 34 | 35 | n = length(x); 36 | x.derivatives = reshape(x.derivatives, n, 1, x.nVariables); 37 | 38 | if nargout < 2 39 | x.values = maxx; 40 | x.derivatives = reshape(x.derivatives(idx,:), 1, 1, x.nVariables); 41 | varargout = {x}; 42 | return; 43 | end 44 | 45 | if idx == 1 46 | dfdp = 1/2*(-3*x.derivatives(idx,:) + 4*x.derivatives(idx+1,:) - x.derivatives(idx+2,:)); 47 | elseif idx == n 48 | dfdp = 1/2*(3*x.derivatives(idx,:) - 4*x.derivatives(idx-1,:) + x.derivatives(idx-2,:)); 49 | else 50 | dfdp = (x.derivatives(idx+1,:) - x.derivatives(idx-1,:)); 51 | end 52 | 53 | if idx <= 2 54 | dfdt = (-x.values(idx+3) + 4*x.values(idx+2) - 5*x.values(idx+1) + 2*x.values(idx)); 55 | elseif idx >= n-1 56 | dfdt = (x.values(idx-3) - 4*x.values(idx-2) + 5*x.values(idx-1) - 2*x.values(idx)); 57 | else 58 | dfdt = 1/12*(-x.values(idx+2) + 16*x.values(idx+1) - 30*x.values(idx) + 16*x.values(idx-1) - x.values(idx-2)); 59 | end 60 | 61 | x.values = maxx; 62 | x.derivatives = reshape(x.derivatives(idx,:), 1, 1, x.nVariables); 63 | 64 | y = x; 65 | y.values = idx; 66 | y.derivatives(1,1,:) = -dfdp/dfdt; 67 | 68 | varargout = {x, y}; 69 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/min.m: -------------------------------------------------------------------------------- 1 | function [x, y] = min(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | % Many problems with this routine 7 | % max neglects the derivative with respect to the time at the moment 8 | % idx has to be rescaled after the output idx = idx.*diff(t(1:2)); 9 | % 10 | % Overall it's a rough approx at the moment, but better than nought. 11 | 12 | if nargout < 2 13 | x = max(-x); 14 | else 15 | [x, y] = max(-x); 16 | end 17 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/minus.m: -------------------------------------------------------------------------------- 1 | function x = minus(x,y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(y, 'myMatrixAD') 7 | if isa(x, 'myMatrixAD') 8 | x.values = x.values - y.values; 9 | x.derivatives = x.derivatives - y.derivatives; 10 | else 11 | y.values = x - y.values ; 12 | y.derivatives = - y.derivatives; 13 | x = y; 14 | end 15 | else 16 | x.values = x.values - y; 17 | end 18 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/mpower.m: -------------------------------------------------------------------------------- 1 | function x = mpower(x,y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | error('Matrix power not implemented, yet. Use ".*" if possible.'); 7 | 8 | if isa(y, 'myMatrixAD') 9 | if isa(x, 'myMatrixAD') 10 | temp = x.values.^(y.values-1); 11 | x.derivatives = valXder(y.values.*temp, x.derivatives) ... 12 | + valXder(temp.*x.values.*log(x.values), y.derivatives); 13 | x.values = temp.*x.values; 14 | else 15 | y.values = x.^y.values; 16 | y.derivatives = valXder(y.values.*log(x), y.derivatives); 17 | x = y; 18 | end 19 | else 20 | x.derivatives = valXder(y.*x.values.^(y-1), x.derivatives); 21 | x.values = x.values.^y; 22 | end 23 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/mtimes.m: -------------------------------------------------------------------------------- 1 | function z = mtimes(x, y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myMatrixAD') 7 | if isa(y, 'myMatrixAD') 8 | z = x; 9 | z.values = x.values*y.values; 10 | % z.derivatives = zeros([size(z.values), z.nVariables]); 11 | % for i = 1:x.nVariables 12 | % z.derivatives(:,:,i) = x.derivatives(:,:,i)*y.values + x.values*y.derivatives(:,:,i); 13 | % end 14 | sx = size(x.values); 15 | sy = size(y.values); 16 | temp1 = reshape(permute(x.derivatives,[1 3 2]), sx(1)*y.nVariables, sx(2)); 17 | temp2 = reshape(y.derivatives, sy(1), sy(2)*y.nVariables); 18 | z.derivatives = permute(reshape(temp1*y.values, sx(1), y.nVariables, sy(2)),[1 3 2]) + reshape(x.values*temp2, sx(1), sy(2), y.nVariables); 19 | else 20 | z = x; 21 | z.values = x.values*y; 22 | % z.derivatives = zeros([size(z.values), z.nVariables]); 23 | % for i = 1:x.nVariables 24 | % z.derivatives(:,:,i) = x.derivatives(:,:,i)*y; 25 | % end 26 | sx = size(x.values); 27 | sy = size(y); 28 | temp1 = reshape(permute(x.derivatives,[1 3 2]), sx(1)*x.nVariables, sx(2)); 29 | z.derivatives = permute(reshape(temp1*y, sx(1), x.nVariables, sy(2)),[1 3 2]); 30 | end 31 | else 32 | z = y; 33 | z.values = x*y.values; 34 | % z.derivatives = zeros([size(z.values), z.nVariables]); 35 | % for i = 1:y.nVariables 36 | % z.derivatives(:,:,i) = x*y.derivatives(:,:,i); 37 | % end 38 | sx = size(x); 39 | sy = size(y.values); 40 | temp2 = reshape(y.derivatives, sy(1), sy(2)*y.nVariables); 41 | z.derivatives = reshape(x*temp2, sx(1), sy(2), y.nVariables); 42 | end 43 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/myMatrixAD.m: -------------------------------------------------------------------------------- 1 | function x = myMatrixAD(x, varargin) 2 | % /* 3 | % * myMatrixAD and myA2D - Automatic Differentiation of 1st and 2nd Derivative 4 | % * Copyright (C) 2006 Martin Fink. (martinfink "at" gmx.at) 5 | % * 6 | % * This library is free software; you can redistribute it and/or 7 | % * modify it under the terms of the GNU General Public License 8 | % * as published by the Free Software Foundation; either version 2.1 9 | % * of the License, or (at your option) any later version. 10 | % * 11 | % * This library is distributed in the hope that it will be useful, 12 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | % * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | % * General Public License for more details. 15 | % * 16 | % * You should have received a copy of the GNU General Public License 17 | % * along with this library; if not, visit 18 | % * http://www.gnu.org/licenses/gpl.html or write to the Free Software 19 | % * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 20 | % */ 21 | 22 | if (isa(x, 'myMatrixAD')) 23 | return; 24 | end 25 | 26 | sx = size(x); 27 | if (length(sx)>2) 28 | error('myMatrixAD allows maximal 2D-matrices.'); 29 | end 30 | 31 | struct_x.nVariables = sx(1)*sx(2); 32 | struct_x.values = x; 33 | if (nargin < 2) 34 | struct_x.derivatives(:,1,:) = eye(struct_x.nVariables); 35 | struct_x.derivatives = reshape(struct_x.derivatives,sx(1),sx(2),sx(1)*sx(2)); 36 | else 37 | struct_x.derivatives = varargin{1}; 38 | struct_x.nVariables = varargin{2}; 39 | end 40 | x = class(struct_x, 'myMatrixAD'); 41 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/ne.m: -------------------------------------------------------------------------------- 1 | function z = ne(x, y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(y, 'myMatrixAD') 7 | if isa(x, 'myMatrixAD') 8 | z = x.values ~= y.values; 9 | else 10 | z = x ~= y.values; 11 | end 12 | else 13 | z = x.values ~= y; 14 | end 15 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/norm.m: -------------------------------------------------------------------------------- 1 | function x = norm(x, p) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if min(size(x))>1 7 | error('Matrix norm not implemented.'); 8 | end 9 | 10 | if nargin==1 11 | p = 2; 12 | end 13 | x = sum(abs(x).^p).^(1/p); 14 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/plus.m: -------------------------------------------------------------------------------- 1 | function x = plus(x, y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(y, 'myMatrixAD') 7 | if isa(x, 'myMatrixAD') 8 | x.values = x.values + y.values; 9 | x.derivatives = x.derivatives + y.derivatives; 10 | else 11 | y.values = x + y.values; 12 | x = y; 13 | end 14 | else 15 | x.values = x.values + y; 16 | end 17 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/power.m: -------------------------------------------------------------------------------- 1 | function x = power(x,y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(y, 'myMatrixAD') 7 | if isa(x, 'myMatrixAD') 8 | temp = x.values.^(y.values); 9 | x.derivatives = valXder(y.values.*x.values.^(y.values-1), x.derivatives) ... 10 | + valXder(temp.*log(x.values), y.derivatives); 11 | x.values = temp; 12 | else 13 | y.values = x.^y.values; 14 | y.derivatives = valXder(y.values.*log(x), y.derivatives); 15 | x = y; 16 | end 17 | else 18 | x.derivatives = valXder(y.*x.values.^(y-1), x.derivatives); 19 | x.values = x.values.^y; 20 | end 21 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/private/valXder.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************\ 2 | * Columnwise array multiplication between Matrix and Vector * 3 | * For myMatAD * 4 | \**************************************************************/ 5 | 6 | #include "mex.h" 7 | #define Out plhs[0] 8 | #define Vec prhs[0] 9 | #define Mat prhs[1] 10 | 11 | void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) 12 | { 13 | if (nlhs > 1) { 14 | mexErrMsgTxt("Too many output arguments."); 15 | } 16 | switch(nrhs) { 17 | default: { 18 | mexErrMsgTxt("Incorrect number of arguments."); 19 | } 20 | case 0: { 21 | mexPrintf("\nColumnwise dot-product matrix with vector.\n\n"); 22 | break; 23 | } 24 | case 2: { 25 | int m=mxGetN(Vec); 26 | int n=mxGetN(Mat); 27 | Out=mxCreateDoubleMatrix(0,0,mxREAL); 28 | if (m > 0 && n > 0) { 29 | int i, j, k=0; 30 | const int* e=mxGetDimensions(Mat); 31 | double* outMat; 32 | double* inMat=mxGetPr(Mat); 33 | double* inVec=mxGetPr(Vec); 34 | mxSetDimensions(Out,e,3); 35 | mxSetPr(Out,outMat=(double*) mxMalloc(e[0]*e[1]*e[2]*sizeof(double))); 36 | if (m == 1) { //scalar time matrix 37 | for (i=0; i1 17 | varargout{2} = idx; 18 | end 19 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/sqrt.m: -------------------------------------------------------------------------------- 1 | function x = sqrt(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = sqrt(x.values); 7 | x.derivatives = valXder(0.5./x.values, x.derivatives); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/subsasgn.m: -------------------------------------------------------------------------------- 1 | function y = subsasgn(y, S, x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | if isempty(S.subs{1}) 6 | return; 7 | end 8 | 9 | % For first run and debugging uncomment: 10 | if (length(S.subs)==1) error('Use 2D matrix encoding'); end 11 | 12 | if isa(y, 'myMatrixAD') 13 | if isa(x, 'myMatrixAD') 14 | y.values(S.subs{:}) = x.values; 15 | y.derivatives(S.subs{:},:) = x.derivatives; 16 | else 17 | y.values(S.subs{:}) = x; 18 | y.derivatives(S.subs{:},:) = 0; 19 | end 20 | else 21 | testDeriv = zeros([size(y), x.nVariables]); 22 | y = myMatrixAD(y, testDeriv, x.nVariables); 23 | y.values(S.subs{:}) = x.values; 24 | y.derivatives(S.subs{:},:) = x.derivatives; 25 | end 26 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/subsref.m: -------------------------------------------------------------------------------- 1 | function x = subsref(x, S) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | % For first run and debugging uncomment: 7 | if (length(S.subs)==1) error('Use 2D matrix encoding'); end 8 | x.values = x.values(S.subs{:}); 9 | x.derivatives = x.derivatives(S.subs{:},:); 10 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/sum.m: -------------------------------------------------------------------------------- 1 | function x = sum(x, dim) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if nargin == 1 7 | s = size(x.values); 8 | if s(1) == 1 9 | dim = 2; 10 | else 11 | dim = 1; 12 | end 13 | end 14 | 15 | x.values = sum(x.values,dim); 16 | x.derivatives = sum(x.derivatives,dim); 17 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/tan.m: -------------------------------------------------------------------------------- 1 | function x = tan(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(1./cos(x.values).^2, x.derivatives); 7 | x.values = tan(x.values); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/tanh.m: -------------------------------------------------------------------------------- 1 | function x = tanh(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.derivatives = valXder(1./(cosh(x.values).^2), x.derivatives); 7 | x.values = tanh(x.values); 8 | 9 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/times.m: -------------------------------------------------------------------------------- 1 | function x = times(x,y) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | if isa(x, 'myMatrixAD') 7 | if isa(y, 'myMatrixAD') 8 | a = length(x.values); 9 | b = length(y.values); 10 | if a == 1 11 | if b == 1 12 | x.derivatives = y.values * x.derivatives + x.values * y.derivatives; 13 | else 14 | x.derivatives = y.values(:,:,ones(x.nVariables,1)) .* x.derivatives + x.values * y.derivatives; 15 | end 16 | else 17 | x.derivatives = y.values(:,:,ones(x.nVariables,1)) .* x.derivatives + x.values(:,:,ones(x.nVariables,1)) .* y.derivatives; 18 | end 19 | x.values = x.values.*y.values; 20 | else 21 | if length(y) == 1 22 | x.derivatives = y * x.derivatives; 23 | else 24 | x.derivatives = y(:,:,ones(x.nVariables,1)) .* x.derivatives; 25 | end 26 | x.values = y.*x.values; 27 | end 28 | else 29 | if length(x) == 1 30 | y.derivatives = x * y.derivatives; 31 | else 32 | y.derivatives = x(:,:,ones(y.nVariables,1)) .* y.derivatives; 33 | end 34 | y.values = x.*y.values; 35 | x = y; 36 | end 37 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/transpose.m: -------------------------------------------------------------------------------- 1 | function x = transpose(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = permute(x.values, [2 1]); 7 | x.derivatives = permute(x.derivatives, [2 1 3]); 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/uminus.m: -------------------------------------------------------------------------------- 1 | function x = uminus(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = - x.values; 7 | x.derivatives = - x.derivatives; 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/uplus.m: -------------------------------------------------------------------------------- 1 | function x = uplus(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/vertcat.m: -------------------------------------------------------------------------------- 1 | function x = vertcat(varargin) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | y = []; i = 1; 7 | while ~isa(varargin{i}, 'myMatrixAD') 8 | y = [y; varargin{i}]; 9 | i=i+1; 10 | end 11 | 12 | x = varargin{i}; 13 | if i>1 14 | x.values = [y; x.values]; 15 | x.derivatives = [zeros([size(y), x.nVariables]); x.derivatives]; 16 | end 17 | 18 | for j = i+1:nargin 19 | if isa(varargin{j}, 'myMatrixAD') 20 | x.values = [x.values; varargin{j}.values]; 21 | x.derivatives = [x.derivatives; varargin{j}.derivatives]; 22 | elseif ~isempty(varargin{j}) 23 | x.values = [x.values; varargin{j}]; 24 | [sx, sy] = size(varargin{j}); 25 | x.derivatives(end+sx, sy, x.nVariables) = 0; 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/@myMatrixAD/zeros.m: -------------------------------------------------------------------------------- 1 | function x = zeros(x) 2 | % In Package myMatrixAD - Automatic Differentiation 3 | % by Martin Fink, June 2006 4 | % martinfink 'at' gmx.at 5 | 6 | x.values = x.values*0; 7 | x.derivatives = x.derivatives*0; 8 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/README.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * myAD, myA2D and myMatrixAD - Automatic Differentiation of 1st and 2nd Order 3 | * Copyright (C) 2006 Martin Fink. (martinfink "at" gmx.at) 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2.1 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this library; if not, visit 17 | * http://www.gnu.org/licenses/gpl.html or write to the Free Software 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 19 | */ 20 | 21 | Dear user, 22 | 23 | This package can be used to calculate first and second derivatives in Matlab(TM). The respective class has to be on the search path of Matlab(TM). 24 | 25 | Usage: 26 | To calculate the derivatives of function myFunc at x0 you have to call 27 | x = myAD(x0); 28 | result = myFunc(x); 29 | functionValue = getvalues(result); 30 | derivatives = getderivs(result); 31 | 32 | If (and only if) you need the second derivative, use the other provided class: 33 | x = myA2D(x0); 34 | result = myFunc(x); 35 | functionValue = getvalues(result); 36 | derivatives = getderivs(result); 37 | secondDerivatives = getsecderiv(result); 38 | 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 40 | 41 | At the moment you cannot use matrix product, power or division. 42 | 43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 44 | 45 | It is recommended to construct solution vectors using vertcat, i.e. 46 | dx = [dx_1); 47 | dx_2; 48 | ... etc.]; 49 | instead of 50 | dx(1) = dx_1; 51 | dx(2) = dx_2; 52 | ... etc. 53 | because the subscript-assignments need much more time than the vertical concatenation. 54 | 55 | Martin Fink 56 | June, 2006 57 | -------------------------------------------------------------------------------- /myAutomaticDifferentiation/example_AD.m: -------------------------------------------------------------------------------- 1 | function example_AD 2 | %% 3 | %% Example for using the classes myAD and myA2D 4 | %% Note that these classes only provide vectors and not matrices 5 | %% 6 | %% myAD and myA2D - Automatic Differentiation of 1st and 2nd Derivative 7 | %% Copyright (C) 2006 Martin Fink (martinfink "at" gmx.at) 8 | %% 9 | %% For further information consider the README file 10 | %% 11 | 12 | t = 7; 13 | %% Normal function use 14 | p = rand(5,1); 15 | f = func(t,p); 16 | 17 | %% Use automatic differentiation when only first order derivative is needed 18 | pAD = myAD(p); 19 | outAD = func(t, pAD); 20 | 21 | %% Use automatic differentiation when first and second order derivative is needed 22 | pA2D = myA2D(p); 23 | outA2D = func(t,pA2D); 24 | 25 | %% Output 26 | % Function values 27 | disp('Function values:'); 28 | f 29 | fAD=getvalue(outAD) 30 | fA2D=getvalue(outA2D) 31 | disp('Press key to continue'); 32 | pause 33 | 34 | % First derivative of function values with respect to parameters 35 | disp(' '); 36 | disp('First derivatives:'); 37 | dfAD=getderivs(outAD) 38 | dfAD=getderivs(outA2D) 39 | disp('Press key to continue'); 40 | pause 41 | 42 | % Second derivative of function values with respect to parameters 43 | disp(' '); 44 | disp('Second derivatives:'); 45 | d2fA2D=getsecderiv(outA2D) 46 | 47 | 48 | function fval = func(t, p) 49 | 50 | fval = [sin(2*pi*t.*p(1)) - t*p(3); 51 | 13 + p(2).*p(3) - p(4).^2; 52 | p(5).*t.^2]; 53 | -------------------------------------------------------------------------------- /real-time-mpc-steering-ucb.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/real-time-mpc-steering-ucb.pdf -------------------------------------------------------------------------------- /trajectory.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngupta1024/FastNMPC/52fdf442e5e015fe918539e84d7ad9e44d1fc192/trajectory.mat --------------------------------------------------------------------------------