Defining variable as a function of other variable
I am trying to define t as a function of k so that when I substitute compute_obj with define_t and then differentiate the objective_with_t function wrt k, Matlab should differentiate variable t for being a function of k. I tried numerous ways to define t as a function of k. Following code shows one of the ways in which I tried to do it –
% Define symbolic variables
syms k t real
% Define the relationship between k and t
t_k = @(k) t; % t is a function of k
% Define the expressions
compute_obj = -(sqrt(1 – k) * (t_k(k)^6 * (2 * m * (3 * m + 2) + 4) – t_k(k)^5 * (m * (m * (5 * m + 6) + 11) + 12) + m^6 + 2 * t_k(k)^7 – t_k(k)^8 – t_k(k)^4 * (m * (3 * m * (m * (3 * m + 2) – 3) – 16) – 9) – 2 * m^3 * t_k(k)^2 * (3 * m^3 + 2 * m + 3) + m^2 * t_k(k)^3 * (m * (3 * m * (5 * m + 4) + 1) – 4)) + sqrt(k) * (t_k(k)^4 * (m * (9 * m * (m * (m + 4) + 5) + 28) + 9) – m^4 * (3 * m – 2) – t_k(k)^6 * (6 * m * (m + 2) + 10) + t_k(k)^5 * (4 * m^3 + 12 * m + 8) + t_k(k)^8 + m * t_k(k)^2 * (m * (m * (4 * m^3 + 3 * m + 9) + 6) – 8) + 2 * m^2 * t_k(k) * (3 * m – 2) – 2 * m * t_k(k)^3 * (m * (m * (6 * m * (m + 2) + 7) + 6) – 2))) / (18 * t_k(k)^3 * (2 * t_k(k) – (m – t_k(k))^2));
define_t = (sqrt(k) * ((m / t + 2) / 3) – sqrt(1 – k) * ((1 / 3) * (2 + (m / t) + ((2 * m + t) / ((m – t)^2 – 2 * t))))) / (sqrt(k) * (2 * m^3 – 3 * (1 + m)^2 * t + t^3) / (3 * ((m – t)^2 – 2 * t)) – sqrt(1 – k) * ((2 * m + t) / 3));
% Main loop to solve for each m
m_values = linspace(0, 1, 100);
k_solutions = zeros(size(m_values));
t_solutions = zeros(size(m_values));
for i = 1:length(m_values)
m = m_values(i);
% Define the objective function with current m and symbolic t
objective_with_t = compute_obj;
% Differentiate the objective function with respect to k
d_obj_d_k = diff(objective_with_t, k);
% Convert the symbolic derivative to a MATLAB function
d_obj_d_k_fn = matlabFunction(d_obj_d_k, ‘Vars’, [k, t]);
% Define a function for numerical root finding to find optimal k
opt_k_fn = @(k_val) d_obj_d_k_fn(k_val, t_k(k_val));
% Use fminbnd to find the optimal k in the range [0.5, 1]
options = optimset(‘Display’, ‘off’);
k_opt = fminbnd(@(k) abs(opt_k_fn(k)), 0.5, 1, options);
% Define a function for numerical root finding to find t_opt
func = matlabFunction(t_k(k) – define_t, ‘Vars’, t);
% Use numerical root finding to find the fixed point of t
try
t_opt = fzero(func, 0.7); % Assuming a starting guess for t
catch
t_opt = NaN;
end
% Store solutions
k_solutions(i) = k_opt;
t_solutions(i) = t_opt;
end
% Display solutions
disp(table(m_values’, k_solutions’, t_solutions’, ‘VariableNames’, {‘m’, ‘k_opt’, ‘t_opt’}));
% Plot results
figure;
plot(m_values, k_solutions, ‘b-‘, ‘LineWidth’, 1.5);
hold on;
plot(m_values, t_solutions, ‘r–‘, ‘LineWidth’, 1.5);
xlabel(‘m’);
ylabel(‘Value’);
legend(‘Optimal k’, ‘Optimal t’);
title(‘Stackelberg Equilibrium Solutions’);
hold off;
I keep getting error. Please someone suggest a way to define t as function of kI am trying to define t as a function of k so that when I substitute compute_obj with define_t and then differentiate the objective_with_t function wrt k, Matlab should differentiate variable t for being a function of k. I tried numerous ways to define t as a function of k. Following code shows one of the ways in which I tried to do it –
% Define symbolic variables
syms k t real
% Define the relationship between k and t
t_k = @(k) t; % t is a function of k
% Define the expressions
compute_obj = -(sqrt(1 – k) * (t_k(k)^6 * (2 * m * (3 * m + 2) + 4) – t_k(k)^5 * (m * (m * (5 * m + 6) + 11) + 12) + m^6 + 2 * t_k(k)^7 – t_k(k)^8 – t_k(k)^4 * (m * (3 * m * (m * (3 * m + 2) – 3) – 16) – 9) – 2 * m^3 * t_k(k)^2 * (3 * m^3 + 2 * m + 3) + m^2 * t_k(k)^3 * (m * (3 * m * (5 * m + 4) + 1) – 4)) + sqrt(k) * (t_k(k)^4 * (m * (9 * m * (m * (m + 4) + 5) + 28) + 9) – m^4 * (3 * m – 2) – t_k(k)^6 * (6 * m * (m + 2) + 10) + t_k(k)^5 * (4 * m^3 + 12 * m + 8) + t_k(k)^8 + m * t_k(k)^2 * (m * (m * (4 * m^3 + 3 * m + 9) + 6) – 8) + 2 * m^2 * t_k(k) * (3 * m – 2) – 2 * m * t_k(k)^3 * (m * (m * (6 * m * (m + 2) + 7) + 6) – 2))) / (18 * t_k(k)^3 * (2 * t_k(k) – (m – t_k(k))^2));
define_t = (sqrt(k) * ((m / t + 2) / 3) – sqrt(1 – k) * ((1 / 3) * (2 + (m / t) + ((2 * m + t) / ((m – t)^2 – 2 * t))))) / (sqrt(k) * (2 * m^3 – 3 * (1 + m)^2 * t + t^3) / (3 * ((m – t)^2 – 2 * t)) – sqrt(1 – k) * ((2 * m + t) / 3));
% Main loop to solve for each m
m_values = linspace(0, 1, 100);
k_solutions = zeros(size(m_values));
t_solutions = zeros(size(m_values));
for i = 1:length(m_values)
m = m_values(i);
% Define the objective function with current m and symbolic t
objective_with_t = compute_obj;
% Differentiate the objective function with respect to k
d_obj_d_k = diff(objective_with_t, k);
% Convert the symbolic derivative to a MATLAB function
d_obj_d_k_fn = matlabFunction(d_obj_d_k, ‘Vars’, [k, t]);
% Define a function for numerical root finding to find optimal k
opt_k_fn = @(k_val) d_obj_d_k_fn(k_val, t_k(k_val));
% Use fminbnd to find the optimal k in the range [0.5, 1]
options = optimset(‘Display’, ‘off’);
k_opt = fminbnd(@(k) abs(opt_k_fn(k)), 0.5, 1, options);
% Define a function for numerical root finding to find t_opt
func = matlabFunction(t_k(k) – define_t, ‘Vars’, t);
% Use numerical root finding to find the fixed point of t
try
t_opt = fzero(func, 0.7); % Assuming a starting guess for t
catch
t_opt = NaN;
end
% Store solutions
k_solutions(i) = k_opt;
t_solutions(i) = t_opt;
end
% Display solutions
disp(table(m_values’, k_solutions’, t_solutions’, ‘VariableNames’, {‘m’, ‘k_opt’, ‘t_opt’}));
% Plot results
figure;
plot(m_values, k_solutions, ‘b-‘, ‘LineWidth’, 1.5);
hold on;
plot(m_values, t_solutions, ‘r–‘, ‘LineWidth’, 1.5);
xlabel(‘m’);
ylabel(‘Value’);
legend(‘Optimal k’, ‘Optimal t’);
title(‘Stackelberg Equilibrium Solutions’);
hold off;
I keep getting error. Please someone suggest a way to define t as function of k I am trying to define t as a function of k so that when I substitute compute_obj with define_t and then differentiate the objective_with_t function wrt k, Matlab should differentiate variable t for being a function of k. I tried numerous ways to define t as a function of k. Following code shows one of the ways in which I tried to do it –
% Define symbolic variables
syms k t real
% Define the relationship between k and t
t_k = @(k) t; % t is a function of k
% Define the expressions
compute_obj = -(sqrt(1 – k) * (t_k(k)^6 * (2 * m * (3 * m + 2) + 4) – t_k(k)^5 * (m * (m * (5 * m + 6) + 11) + 12) + m^6 + 2 * t_k(k)^7 – t_k(k)^8 – t_k(k)^4 * (m * (3 * m * (m * (3 * m + 2) – 3) – 16) – 9) – 2 * m^3 * t_k(k)^2 * (3 * m^3 + 2 * m + 3) + m^2 * t_k(k)^3 * (m * (3 * m * (5 * m + 4) + 1) – 4)) + sqrt(k) * (t_k(k)^4 * (m * (9 * m * (m * (m + 4) + 5) + 28) + 9) – m^4 * (3 * m – 2) – t_k(k)^6 * (6 * m * (m + 2) + 10) + t_k(k)^5 * (4 * m^3 + 12 * m + 8) + t_k(k)^8 + m * t_k(k)^2 * (m * (m * (4 * m^3 + 3 * m + 9) + 6) – 8) + 2 * m^2 * t_k(k) * (3 * m – 2) – 2 * m * t_k(k)^3 * (m * (m * (6 * m * (m + 2) + 7) + 6) – 2))) / (18 * t_k(k)^3 * (2 * t_k(k) – (m – t_k(k))^2));
define_t = (sqrt(k) * ((m / t + 2) / 3) – sqrt(1 – k) * ((1 / 3) * (2 + (m / t) + ((2 * m + t) / ((m – t)^2 – 2 * t))))) / (sqrt(k) * (2 * m^3 – 3 * (1 + m)^2 * t + t^3) / (3 * ((m – t)^2 – 2 * t)) – sqrt(1 – k) * ((2 * m + t) / 3));
% Main loop to solve for each m
m_values = linspace(0, 1, 100);
k_solutions = zeros(size(m_values));
t_solutions = zeros(size(m_values));
for i = 1:length(m_values)
m = m_values(i);
% Define the objective function with current m and symbolic t
objective_with_t = compute_obj;
% Differentiate the objective function with respect to k
d_obj_d_k = diff(objective_with_t, k);
% Convert the symbolic derivative to a MATLAB function
d_obj_d_k_fn = matlabFunction(d_obj_d_k, ‘Vars’, [k, t]);
% Define a function for numerical root finding to find optimal k
opt_k_fn = @(k_val) d_obj_d_k_fn(k_val, t_k(k_val));
% Use fminbnd to find the optimal k in the range [0.5, 1]
options = optimset(‘Display’, ‘off’);
k_opt = fminbnd(@(k) abs(opt_k_fn(k)), 0.5, 1, options);
% Define a function for numerical root finding to find t_opt
func = matlabFunction(t_k(k) – define_t, ‘Vars’, t);
% Use numerical root finding to find the fixed point of t
try
t_opt = fzero(func, 0.7); % Assuming a starting guess for t
catch
t_opt = NaN;
end
% Store solutions
k_solutions(i) = k_opt;
t_solutions(i) = t_opt;
end
% Display solutions
disp(table(m_values’, k_solutions’, t_solutions’, ‘VariableNames’, {‘m’, ‘k_opt’, ‘t_opt’}));
% Plot results
figure;
plot(m_values, k_solutions, ‘b-‘, ‘LineWidth’, 1.5);
hold on;
plot(m_values, t_solutions, ‘r–‘, ‘LineWidth’, 1.5);
xlabel(‘m’);
ylabel(‘Value’);
legend(‘Optimal k’, ‘Optimal t’);
title(‘Stackelberg Equilibrium Solutions’);
hold off;
I keep getting error. Please someone suggest a way to define t as function of k function, optimization, fmincon MATLAB Answers — New Questions