Defining variable as a function of other variable

9 views (last 30 days)
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
Torsten
Torsten on 21 Jun 2024
Edited: Torsten 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)
d_obj_d_k(k) = 

Sign in to comment.

Answers (0)

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!