clc, clear all, close all
%% Part 1
%% Question 1
syms q p
C(q) = 610 - 0.2*q + 0.15*q^2; % Cost function
D(p) = 260 - 20*p; % Demand function
% Revenue is equal to the amount of units multiplied by the demand (price)
% function
R(p) = p*D(p)
% The profit is equal to the Revenue minus the Cost
P(p) = R(p) - subs(C(q), q, D(p))
% The marginal cost is the first derivative of the cost function
MC(q) = diff(C, q)
% The average cost is the Cost function divided by the total units
AC(q) = C(q)/q
% The marginal revenue is the first derivative of the Revenue function
MR(p) = diff(R,p);
%% Question 2
figure
ps = 0:0.1:13;
plot(ps, D(ps), 'linewidth', 2);
xlabel('Price ($)');
ylabel('Demand (q)');
title('Demand vs. Price');
grid on
%% Question 3
figure
subplot(1,2,1)
qs = 1:1:D(0);
plot(qs, C(qs), 'linewidth', 2), grid on
xlabel('Quantity (q)');
ylabel('Cost ($)');
title('Cost, Marginal Cost, Marginal Revenue and Average Cost vs. Quantity');
hold on
plot(qs, MC(qs), 'linewidth', 2);
plot(qs, AC(qs), 'linewidth', 2);
plot(qs, MR(qs), 'linewidth', 2);
legend('Cost', 'Marginal Cost', 'Average Cost', 'Marginal Revenue');
subplot(1,2,2)
plot(qs, MC(qs), 'linewidth', 2), hold on;
plot(qs, AC(qs), 'linewidth', 2);
plot(qs, MR(qs), 'linewidth', 2);
legend('Marginal Cost', 'Average Cost', 'Marginal Revenue');
xlabel('Quantity (q)');
ylabel('Cost ($)');
title('Marginal Cost, Average Cost and Marginal Revenue vs. Quantity');
grid on
%% Question 4
% To calculate the point of elasticity of demand, we select 2 points of the
% curve
% For example, we selec the points (8, 100) and (9, 80)
% Change in quantity
q_change_perc = (9-8)/(9+8)/2 *100;
p_change_perc = (80-100)/(80+100)/2 *100;
p_elast_D = abs(q_change_perc/p_change_perc)
% Because the value is < 1, the demand is inelastic
% Now, we will calculate the intervals from P = 1 to 13, where demand is
% elastic and inelastic
pvec = 0:0.01:13;
N = length(pvec);
elast_vec = [];
for i = 1:N-1
P1 = [pvec(i) D(pvec(i))];
P2 = [pvec(i+1), D(pvec(i+1))];
q_change_perc = (P2(1)-P1(1))/(P2(1)+P1(1))/2 *100;
p_change_perc = (P2(2)-P1(2))/(P2(2)+P1(2))/2 *100;
p_elast_D = abs(q_change_perc/p_change_perc);
elast_vec = [elast_vec, p_elast_D];
end
temp = pvec(find(elast_vec > 1));
elastic_interval = [temp(1), temp(end)]
temp = pvec(find(elast_vec < 1));
inelastic_interval = [temp(1), temp(end)]
temp = pvec(find(elast_vec == 1));
if length(temp)>0
unitary_interval = [temp(1), temp(end)]
else
unitary_interval = []
end
%% Question 6
% The revenue is a negative parabola, so we find its vertex
a = -20;
b = 260;
c = 0;
pmax = -b/(2*a);
qmax = subs(D,p,pmax);
%% Question 7
% The Average Cost is a function that is inversely proportional to the
% quantity
% To minimize this function, we will use the MATLAB's function fmincon
% We set the bounds for the quantity to: 0 < q < Inf
type ObjectiveAverageCost
[qmin, fval, exiflag] = fmincon('ObjectiveAverageCost', 10, [], [], [], [], 0, Inf);
qmin_int = round(qmin)
pmin = double(solve(D==qmin))
pmin_int = double(solve(D==qmin_int))
%% Question 8
% The profit function is a negative parabola, so the price that maximizes
% it is its vertex
a = -80;
b = 1816;
p_profit_max = -b/(2*a)
q_profit_max = D(p_profit_max)
%% Part 3
%% Question 10
% This time, the supply function is a price function, where the independent
% variable is the quantity.
% we can express the quantity in function of price as:
S(p) = p-8;
% The equilibrium point is where the demand and supply are the same
p_equil = solve(D == S)
q_equil = D(p_equil)
figure
ps = 0:0.1:20;
plot(ps, D(ps), 'linewidth',2), hold on
plot(ps, S(ps), 'linewidth', 2)
plot(p_equil, D(p_equil), 'r*', 'linewidth', 2);
xlabel('Price ($)');
ylabel('Quantity (q)');
title('Supply and Demand vs. Price');
legend('Demand', 'Supply');
grid on
%% Question 11
% The consumer surplus is the integral between the equilibrium point and
% the Demand function
CS = int((D(p)-q_equil), p, 0, p_equil)
%% Question 12
% The producer surplus is the integral between the Supply function and the
% equilibrium point
PS = int((q_equil - S(p)), p, 0, p_equil)
function f = ObjectiveAverageCost(x)
f = 3*x/20 + 610/x - 1/5;
end
% The code you used to get the answer:
n = 10;
digitsWhole = digits(n+(numel(num2str(floor(pi)))));
MyPi = vpa(pi);
digits_after =char(rem(MyPi,1)); % 10 digits as required
ref = strfind(digits_after,'.');
tenth_digit = str2num(digits_after(ref+n));
% The 10th digit of pi after the decimal is:
fprintf('The 10th digit of pi after the decimal is: %g \n' ,tenth_digit)
% The value of y didn't appear in the command window because:
% *It was suppressed with a semicolon (;)*
% Your code:
horizontal_vec = [8 2 6 10]
% Your code:
vertical_vec = [5; 9; -9; 0]
%OR: vertical_vec = [5 9 -9 0]'
% Your code:
k=1;
for i=98:-1:42
if mod(i,2)~=0 && i~=1
odd(k)=i;
k=k+1;
end
end
odd % expected horizontal vector
num2str(odd)
% Your code:
k=1;
for i=5:1:101
if mod(i,2)==0 && i~=1
even(k)=i;
k=k+1;
end
end
even % expected horizontal vector
num2str(even)
% The code you used to get the answer:
X = 10:1.5:175;
Number_of_element_in_X = numel(X);
% The answer is:
fprintf('The answer is: %g \n', Number_of_element_in_X)
% Your code:
n=36;
a= 23;
b = 27;
list = linspace(a,b,n)';
num2str(list)
% The code you used to create a 5 x 4 matrix of random numbers between -2
% and 5:
a = -2;
b = 5;
m= 5; n=4;
Mat_X = a + (b-a).*rand(m,n)
% The code you used to determine the number in the 2nd row and 4th column:
Mat_X(2,4)
% The code you used to create a 3 x 7 matrix of random integers between -8
% and -2:
a = -8;
b = 2;
m= 3; n=7;
Mat_Y = randi([a,b], m, n)
% The code you used to slice out the 2nd and 3rd rows:
j = 1; % removes 2nd and 3rd rows
Mat_Y_new = Mat_Y(j,:)
% The code you used to create a 3 x 4 matrix of 0's:
Mat_A = zeros(3,4);
% The code you used to create a 3 x 4 matrix of 6's:
Mat_B = 6* ones(3,4);
% The code you used to combine them horizontally:
Mat_C = horzcat(Mat_A, Mat_B)
% The code you used to combine them vertically:
Mat_D = vertcat(Mat_A, Mat_B)
% Your code:
m = 3;
n = 700;
for i = 1:m
for j = 1:n
Mat_E(i,j) = j;
end
end
Mat_E
% Alternatively:
% Mat_E = [1:700].*ones(3,1)
% Your code:
m = 850;
n = 850;
for i = 1:m
for j = 1:n
Mat_F(j,j) = j;
end
end
Mat_F;
% Alternatively:
% Mat_F = diag([1:850])