numerical analysis 0909 question 1: Write the “cournot” function to return the function value fval and the jacobian value fjac for the system of first-order conditions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
%matlab code: %create a function named "cournot" function[fval, fjac] = cournot(q)
%set the parameters c=[0.6;0.8];eta=1.6;e=-1/eta;
%define the fval, f here is first-order equations exactly. fval =sum(q)^e+e*sum(q)^(e-1)*q-diag(c)*q;
%define the fjac fjac =e*sum(q)^(e-1)*ones(2,2)+e*sum(q)^(e-1)*eye(2)+(e-1)*e*sum(q)^(e-2)*q*[11]-diag(c);
%main file q=newton('cournot', [0.2;0.2]);
%get the answer %[0.839567603529586;0.688796431162672]
qusetion 2: Write the “cournot2” function to return the same output as in E1, but using symbolic expressions in MATLAB
%matlab code %create a function named "cournot2" function[fval, fjac] = cournot2(q)
%set the parameters c=[0.6;0.8];eta=1.6;e=-1/eta;
%define the symbolic expressions syms q1 q2;
%define the function f f=[(q1+q2)^e+e*(q1+q2)^(e-1)*q1-c(1)*q1;(q1+q2)^e+e*(q1+q2)^(e-1)*q2-c(2)*q2];
%define the jacobian J=jacobian(f,[q1,q2]);
%get the actual value of f and J at certain points fval=double(subs(f,[q1 q2],[q(1) q(2)]));fjac=double(subs(J,[q1 q2],[q(1) q(2)]));
%main file q=newton('cournot2',[0.2;0.2]);
%get the answer %[0.839567603529586;0.688796431162672]
question 3: In your main file, write the algorithm to implement the newton method matlab code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
%main file %set the tolerance tol=1e-8; %give the initial guess q=[0.2;0.2];
%set the for loop for it=1:100 %get the fval and fjac from cournot2 [fval,fjac]=cournot2(q); %use the newton method to update q=q-fjac\fval; %check whether the root is closer enough if norm(fval)<tol, break, end
end
%get the answer %[0.839567603535660;0.688796431163001]
question 4: Solve the Cournot game, by considering a planner.
%just derive the new first-order equations and put them into the code of cournot2 %create a function named "cournot3" function[fval, fjac] = cournot3(q)
%set the parameters c=[0.6;0.8];eta=1.6;e=-1/eta;
%define the symbolic expressions syms q1 q2;
%define the function f f=[(1+e)*(q1+q2)^e-c(1)*q1;(1+e)*(q1+q2)^e-c(2)*q2];
%define the jacobian J=jacobian(f,[q1,q2]);
%get the actual value of f and J at certain points fval=double(subs(f,[q1 q2],[q(1) q(2)]));fjac=double(subs(J,[q1 q2],[q(1) q(2)]));
%main file q=newton('cournot3',[0.2;0.2]);
%get the answer %[0.603825598888436;0.452869199166327]