% Lab #8 on 27-Mar-08 clear, clc, home fprintf('The date and time: %s \n', datestr(now))
The date and time: 08-Apr-2008 13:58:07
% Driver % % Inputs % tspan : interval % initcon: initial conditions % parameters: inputs besides t and y in the user-defined funtion. % %
clear, clc, home fprintf('The date and time: %s \n \n', datestr(now)) disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') disp('% %') disp('% Purpose: Solve 1st order 12.12 (c) %') disp('% %') disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') % Solve: dy/dt = at + bty; y(0) = 1 Tspan = [0 20]; initc = 1; a = 1; b = 1; [T, Y] = ode45(@SolveODE1st,Tspan,initc,[],a,b); % Anilytical function. t = [0:.1:20]; y = -1/b.*(a - (a + b).*exp((b.*t.^2)./2)); plot(T, log(Y),t, log(y)), legend('Numerical MATLAB', 'Exact')
The date and time: 08-Apr-2008 13:58:08 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % Purpose: Solve 1st order 12.12 (c) % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('\nDirrectory is: \n') dir load SimDEfirst.mat fprintf('\nDisplay all variables: \n') whos % Anilytical function. t = [0:.1:20]; a = 1; b = 1; y = -1/b.*(a - (a + b).*exp((b.*t.^2)./2)); % Plot both plot(yDEfirst(1,:), yDEfirst(2,:),t, log(y)), legend('Numerical Simulink', 'Exact')
Dirrectory is: . ReckExample12_2.m SimSolveDE1st.mdl SolveODE3rd.asv rhs1.m .. SimDEfifth.mat SimSolveDE3rd.mdl SolveODE3rd.m Lab8.asv SimDEfirst.mat SimSolveDE5th.mdl SolveODE5th.m Lab8.m SimDEthird.mat SolveODE1st.m html Display all variables: Name Size Bytes Class T 677x1 5416 double array Tspan 1x2 16 double array Y 677x1 5416 double array a 1x1 8 double array b 1x1 8 double array initc 1x1 8 double array t 1x201 1608 double array y 1x201 1608 double array yDEfirst 2x175 2800 double array Grand total is 2111 elements using 16888 bytes
Simulink Model:
clear, clc, home fprintf('The date and time: %s \n \n', datestr(now)) disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') disp('% %') disp('% Purpose: solve 3rd order ODE IVP %') disp('% %') disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') fprintf('\nSolution obtained from: http://answers.yahoo.com/question/index?qid=20080324172753AAhCKCQ') % Solve a*y''' + b*y' = 0, y''(0) = 1, y'(0) = 0, y(0) = 1 % y''' = -b/a*y' tspan = [0 10]; initcon = [1, 0, 1]; p = [1, 1]; [T,Y]= ode45(@SolveODE3rd, tspan, initcon, [], p); % figure(1) % plot(T, Y(:,1)) % Analitic solution t = [0:0.1:10]; y = [2 - cos(t)]; % figure(2) % plot(t, y) % Plot both figure(3) plot(T, Y(:,1), t, y), legend('Numerical Matlab', 'Exact')
The date and time: 08-Apr-2008 13:58:11 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % Purpose: solve 3rd order ODE IVP % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Solution obtained from: http://answers.yahoo.com/question/index?qid=20080324172753AAhCKCQ
fprintf('\nDirrectory is: \n') dir load SimDEthird.mat fprintf('\nDisplay all variables: \n') whos % Analitic solution t = [0:0.1:10]; y = [2 - cos(t)]; % Plot both plot(yDEthird(1,:), yDEthird(2,:), t, y), legend('Numerical Simulink', 'Exact')
Dirrectory is: . ReckExample12_2.m SimSolveDE1st.mdl SolveODE3rd.asv rhs1.m .. SimDEfifth.mat SimSolveDE3rd.mdl SolveODE3rd.m Lab8.asv SimDEfirst.mat SimSolveDE5th.mdl SolveODE5th.m Lab8.m SimDEthird.mat SolveODE1st.m html Display all variables: Name Size Bytes Class T 73x1 584 double array Y 73x3 1752 double array initcon 1x3 24 double array p 1x2 16 double array t 1x101 808 double array tspan 1x2 16 double array y 1x101 808 double array yDEthird 2x56 896 double array Grand total is 613 elements using 4904 bytes
Simulink Model:
clear, clc, home fprintf('The date and time: %s \n \n', datestr(now)) disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') disp('% %') disp('% Purpose: solve 4th order ODE IVP %') disp('% %') disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') fprintf('\nSolution obtained from: http://ltcconline.net/greenl/courses/204/appsHigherOrder/HigherConstants.htm') % Solve: a*y''''' - b*y''' = 0 % With IV: y(0) = 2, y'(0) = 3, y''(0) = 1, y'''(0) = -1, y''''(0) = 1 % y''''' = b/a*y''' tspan = [0 10]; initcon = [1, -1, 1, 3, 2]; p = [1, 4]; [T,Y]= ode45(@SolveODE5th, tspan, initcon, [], p); % Analitic solution t = [0:0.1:10]; y = 31/16 + (23/4).*t + (5/8).*t.^2 + (1/16).*cos(2.*t) + (1/8).*sin(2.*t); % Plot both plot(T, Y(:,1), t, y), legend('Numerical Matlab', 'Exact')
The date and time: 08-Apr-2008 13:58:15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % Purpose: solve 4th order ODE IVP % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Solution obtained from: http://ltcconline.net/greenl/courses/204/appsHigherOrder/HigherConstants.htm
fprintf('\nDirrectory is: \n') dir load SimDEfifth.mat fprintf('\nDisplay all variables: \n') whos % Analitic solution t = [0:0.1:10]; y = 31/16 + (23/4).*t + (5/8).*t.^2 + (1/16).*cos(2.*t) + (1/8).*sin(2.*t); % Plot both plot(yDEfifth(1,:), yDEfifth(2,:), t, y), legend('Numerical Simulink', 'Exact')
Dirrectory is: . ReckExample12_2.m SimSolveDE1st.mdl SolveODE3rd.asv rhs1.m .. SimDEfifth.mat SimSolveDE3rd.mdl SolveODE3rd.m Lab8.asv SimDEfirst.mat SimSolveDE5th.mdl SolveODE5th.m Lab8.m SimDEthird.mat SolveODE1st.m html Display all variables: Name Size Bytes Class T 81x1 648 double array Y 81x5 3240 double array initcon 1x5 40 double array p 1x2 16 double array t 1x101 808 double array tspan 1x2 16 double array y 1x101 808 double array yDEfifth 2x52 832 double array Grand total is 801 elements using 6408 bytes
Simulink Model: