%% Project C - solving an initial problem % please write your "candidate number" here: % please do NOT write your name or college in this file clc clear all close all %% 5.1 Matlab's symbolic toolbox syms t y f(t,y) = sin((t^2)*y); ob = matlabFunction(f,'vars',[t,y]); t0 = [0:0.1:8]; Sol = ob(t0,1); int = sum(Sol); figure plot(t0,Sol,'-bo') xlabel('t','fontsize',15) ylabel('y','fontsize',15) title('Solving Function sin(t^2)*y Using symbolic toolbox','fontsize',15) %% 5.2 trapezium rule n = 81; lowerLimit = 0; upperLimit = 8; % Discretization into equally spaced segments x = linspace(lowerLimit,upperLimit,n+1); % Calculate the function y y = sin(x.^2); % Trapezoidal formula A = 0; for i=2:n A = A + y(i); end M = 2*A; I = (upperLimit-lowerLimit)*(y(1)+M+y(end))/(2*n); figure plot(x,y,'-bo') xlabel('t','fontsize',15) ylabel('y','fontsize',15) title('Solving Function sin(t^2)*y Using trapezium rule','fontsize',15) %% 5.3 % Excercise_5.3 Solution % ====================== tspan = [0 8]; y0 = 1; [t,y] = ode23s(@(t,y) sin(t^2)*y, tspan, y0); figure plot(t,y,'-bo') title('Solving Function sin(t^2)*y Using ODE23s','fontsize',15) grid on xlabel('t','fontsize',15) ylabel('y','fontsize',15) %% 5.4 n = 81; t0 = 0; T = 8; y0 = 1; % Calculate the function y h = (T - t0) / n; % [t, y] = Heun(f, t0, T, h, y0); [t,y] = Heun(inline('sin(y^2*t)','t','y'),t0,T,h,y0); figure plot(t,y,'-bo') title('Solving Function sin(t^2)*y Using Heun method','fontsize',15) grid on xlabel('t','fontsize',15) ylabel('y','fontsize',15)