40 likes | 154 Views
Problems and solutions Session 4. Problems. Implement the minimum search of previous examples (Way 1-4). Write the previous ode –example as an anonymous function handle call. Compute the integral ∫ [0,1] F(x) dx, F(x) = sqrt(x)/exp(x^2), with quad. Problems – my function function.
E N D
Problems • Implement the minimum search of previous examples (Way 1-4). • Write the previous ode –example as an anonymous function handle call. • Compute the integral ∫[0,1] F(x) dx, F(x) = sqrt(x)/exp(x^2), with quad. Introduction to MATLAB - Solutions 4
Problems – my function function • Write a function differf.m that computes the differential of the argument function f:RnRm at a given point: function df = differf(fff,x,h) Here, fff is the function argument x is the point in Rn at which the differential is computed h is the small number in the difference quotience Recall, a differential at x is a linear matrix Df(x) with f(x+h) = f(x) + Df(x)h + error(x,h), and the j’th column of Df(x) can be computed as Df(x)(:,j) = lim (f(x+hej)-f(x))/h. Test your differf with a) f(x) = x^2+3x, b) f(x) = Ax, A = rand(4,5). Introduction to MATLAB - Solutions 4
[t,y] = ode23(@(t,y) [y(2);y(2)+y(1)],[0,1],[0;1]); integral = quad(@(x) sqrt(x)./exp(x.^2),0,1); function df = differf(fff,x,h) n = length(x); % dimension of the starting space fx = fff(x); % value at x – computed only once! m = length(fx); % dimension of the goal space df = zeros(m,n); % reserve the memory space for df for j = 1:n ej = zeros(n,1); ej(j) = 1; df(:,j) = (fff(x+h*ej)-fx)/h; end CALL differf(@(x) x.^2+3*x,1,0.0001) % derivative at x=1 A = randn(4,5) dA = differf(@(x) A*x, ones(5,1), 0.0001) % derivative at x=ones(5,1) % dA should be A !! Some solutions Introduction to MATLAB - Solutions 4