% gradient descent quadratic bowl test clc %clear console and return cursor to home format long %actual function tm = linspace(0,10,100); N0 = 10; R0 = 0.75; Nd_smth = N0*exp(R0*tm); M = length(Nd_smth); %generate vals from random distr with mean = mn and stdv = stdv rng('default'); %fix seed for random # generator to matlab startup seed mn = 0; %mean stdv = 1000; r_pb = mn + stdv.*randn(1,M); Nd = r_pb+Nd_smth; %data plot(tm,Nd,'bx') N = @(N0,R0,t) N0.*exp(R0*t); F = @(N0,R0,t) 0.5*sum( (N(N0,R0,t)-Nd).^2 ); F_N0 = @(N0,R0,t) sum( (N(N0,R0,t)-Nd).*exp(R0*t) ); F_R0 = @(N0,R0,t) sum( (N(N0,R0,t)-Nd).*(N0*t.*exp(R0*t)) ); % initial guess N0 = 100; R0 = 0.01; lambda = 1000; delta = 1e-9; %timestep or learning rate N_max = 600000; Tol = 1e-3; plot(tm,Nd,'bx',tm,N(N0,R0,tm),'r-') pause; Out_max = 10; for i = 1:N_max N0_old = N0; N0 = N0 - delta*(F_N0(N0,R0,tm)); R0 = R0 - (1/lambda)*delta*(F_R0(N0_old,R0,tm)); %plot curve every 5000 iterations if rem(i,5000) == 0 sprintf("N0=%f, R0=%f, i=%f",N0,R0,i) pause(0.1) plot(tm,Nd,'bx',tm,N(N0,R0,tm),'r-') end end disp(N0) disp(R0) plot(tm,Nd,'bx',tm,N(N0,R0,tm),'r-')