ans(k) =
Defining variable as a function of other variable
Show older comments
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
9 Comments
Example:
syms k t(k)
compute_obj = k+t;
diff(compute_obj,k)
I don't understand how define_t comes into play.
Sabrina Garland
on 21 Jun 2024
Torsten
on 21 Jun 2024
So you want to solve define_t(k,t) == 0 for t, insert this expression that now only depends on k into compute_obj and differentiate compute_obj with respect to k ? This will be difficult since define_t(k,t) == 0 as a function of k will have many different solutions.
Why don't you go the way I suggested ?
Sabrina Garland
on 21 Jun 2024
Sabrina Garland
on 21 Jun 2024
Before continuing with complicated analytical methods, consider the following situation:
You have two profit functions qL(k,t) and qF(k,t) for leader and follower where k is the decision variable for the leader and t is the decision variable for the follower.
Make a loop over all possible values of k in which you compute the optimum response t(k) of the follower ( i.e. the response t(k) that maximizes his profit qF(k,t(k)) ).
For each such pair (k,t(k)), compute the profit qL(k,t(k)) of the leader.
Take the combination (k*,t(k*)) = (k*,t*) as optimal that maximizes qL(k,t(k)).
The only questions remaining in your case are:
What are qL and qF ?
What is the possible range of k to loop over ?
What is the possible range of t to possibly restrict the action of the follower ?
Sabrina Garland
on 21 Jun 2024
Why does t(k) operation doesn't work with substitution? Is it because the equation becomes too complex?
I already showed you how it works with the simple example I included above. But I don't understand what would be the purpose of this differentiation.
syms k t(k) m
compute_obj = -(sqrt(1 - k) * (t^6 * (2 * m * (3 * m + 2) + 4) - t^5 * (m * (m * (5 * m + 6) + 11) + 12) + m^6 + 2 * t^7 - t^8 - t^4 * (m * (3 * m * (m * (3 * m + 2) - 3) - 16) - 9) - 2 * m^3 * t^2 * (3 * m^3 + 2 * m + 3) + m^2 * t^3 * (m * (3 * m * (5 * m + 4) + 1) - 4)) + sqrt(k) * (t^4 * (m * (9 * m * (m * (m + 4) + 5) + 28) + 9) - m^4 * (3 * m - 2) - t^6 * (6 * m * (m + 2) + 10) + t^5 * (4 * m^3 + 12 * m + 8) + t^8 + m * t^2 * (m * (m * (4 * m^3 + 3 * m + 9) + 6) - 8) + 2 * m^2 * t * (3 * m - 2) - 2 * m * t^3 * (m * (m * (6 * m * (m + 2) + 7) + 6) - 2))) / (18 * t^3 * (2 * t - (m - t)^2));
d_obj_d_k = diff(compute_obj, k)
Sabrina Garland
on 21 Jun 2024
Answers (0)
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!