%%%%%%%%%%%%%%%%%%
% Question 1
%%%%%%%%%%%%%%%%%%
Copyright By PowCoder代写 加微信 powcoder
clear all;
%%%%%%%%%%%%%%%%%%
% Question 2
%%%%%%%%%%%%%%%%%%%%
% The function is defined at the bottom
fprintf(‘—– Question 2 ———\n’);
fprintf(‘The value of the quadratic function at (1,1) is %4.2f \n’, quadratic_function([1,1]));
fprintf(‘The value of the quadratic function at (3,2) is %4.2f \n’, quadratic_function([3,2]));
%%%%%%%%%%%%%%%%%
% Question 3
%%%%%%%%%%%%%%%%%
% fminsearch finds the x value which minimizes the function
% by iterating (from an initial value x0) until the minimum is found
% Here, we do not display the iterations
x0 = [2,10]; % starting value (try different starting values …)
options = optimset(‘Display’,’off’);
[xmin,fmin] = x0, options);
fprintf(‘—- Question 3 ———- \n’)
fprintf(‘The value of the minimizer (first component) is %2.2f \n’, xmin(1));
fprintf(‘The value of the minimizer (second component) is %2.2f \n’, xmin(2));
fprintf(‘The value of the function at the minimum is %4.2f \n’, fmin);
%%%%%%%%%%%%%%%
% Question 4
%%%%%%%%%%%%%%%
% fminsearch finds the x value which minimizes the function
% by iterating (from an initial value x0) until the minimum is found
% Here, we display the iterations
x = [1 2 3 4]’;
A = eye(4);
theta0 = [20 20 20 20]’; % starting value (try different starting values …)
options = optimset(‘Display’, ‘iter’);
[thetamin,fmin] = theta0, options,x,A);
fprintf(‘—- Question 4 ———- \n’)
fprintf(‘The value of the minimizer (first component) is %2.2f \n’, thetamin(1));
fprintf(‘The value of the minimizer (second component) is %2.2f \n’, thetamin(2));
fprintf(‘The value of the minimizer (third component) is %2.2f \n’, thetamin(3));
fprintf(‘The value of the minimizer (fourth component) is %2.2f \n’, thetamin(4));
fprintf(‘The value of the function at the minimum is %4.2f \n’, fmin);
%%%%%%%%%%%%%%%%
% Question 5
%%%%%%%%%%%%%%%
x = [1 2 3 4]’;
A = [1 0.5 0.2 0.3; 0.5 1 0.4 0.5; 0.2 0.4 1 0.2; 0.3 0.5 0.2 1];
theta0 = [20 20 20 20]’; %initial value
options = optimset(‘Display’, ‘off’);
[thetamin,fmin,exitflag,output] = theta0, options,x,A);
fprintf(‘—- Question 5 ———- \n’)
fprintf(‘The value of the minimizer (first component) is %2.2f \n’, thetamin(1));
fprintf(‘The value of the minimizer (second component) is %2.2f \n’, thetamin(2));
fprintf(‘The value of the minimizer (third component) is %2.2f \n’, thetamin(3));
fprintf(‘The value of the minimizer (fourth component) is %2.2f \n’, thetamin(4));
fprintf(‘The value of the function at the minimum is %4.2f \n’, fmin);
%%%%%%%%%%%%%%%
% Question 6
%%%%%%%%%%%%%%%
% see below function like_bernoulli
%%%%%%%%%%%%%%
% Question 7
%%%%%%%%%%%%%%
T = 10000;
x = random(‘Binomial’, 1, 0.3, 1, T)’;
options = optimset(‘Display’, ‘iter’);
thetahat = .2, options, x);
fprintf(‘—- Question 7 ———- \n’)
fprintf(‘The value of the minimizer is %2.3f \n’, thetahat);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function to solve 1, 2 and 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f = quadratic_function(x)
if length(x)>2
printf(“Error, x should be a 2-dimensional vector”)
f = x(1)^2 + 3*x(2)^2 + 2*x(1)*(x(2)^0.5);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function to solve 4 and 5
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f = quadratic_form(theta,x,A)
f = (x-theta)’*A*(x-theta);
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function to solve 6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function L=like_bernoulli1(theta,x)
L = mean( x.*log(theta) + (1-x).*log(1-theta) );
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function to solve 7
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function L=like_bernoulli2(theta,x)
L = -mean( x.*log(theta) + (1-x).*log(1-theta) );
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com