Defining variable as a function of other variable (2024)

Sabrina Garland on 21 Jun 2024 at 10:49

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable

Commented: Sabrina Garland on 21 Jun 2024 at 18:14

Open in MATLAB Online

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);

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

Show 7 older commentsHide 7 older comments

Torsten on 21 Jun 2024 at 12:23

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192441

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192441

Edited: Torsten on 21 Jun 2024 at 12:25

Open in MATLAB Online

Example:

syms k t(k)

compute_obj = k+t;

diff(compute_obj,k)

ans(k)=

Defining variable as a function of other variable (3)

I don't understand how define_t comes into play.

Sabrina Garland on 21 Jun 2024 at 14:27

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192551

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192551

@Torsten Thanks for your reply . There are two equations - compute_obj and define_t. I want to optimize for k after substituting define_t into compute_obj and then differentiating the obtained objective function, called as objective_with_t, wrt k. But I always get an error. Please help how can I make it work in my system

Torsten on 21 Jun 2024 at 14:37

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192581

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192581

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 at 14:54

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192621

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192621

@Torsten define_t is an expression

 t= (((k)^(1/2))*(((m/t)+2)/3) - ((1-k)^(1/2))*((1/3)(2+ (m/t)+((2m+t)/(((m-t)^2)-2t)))))/(((k)^(1/2))*(2(m^3) -3((1+m)^2)t+t^3)/(3(((m-t)^2)-2t)) - ((1-k)^(1/2))*((2m+t)/3))

Note above t expression is a function of k. Since I can't explicitly express t as a function of k, I'll have t as a function of k in an implicit and explicit way. So, I substitute this define_t (t) into compute_obj (replacing t with this expression) and optimizing for k. I am substituting so as to undertake the effect k has on t, even though partially (had I have an explicit equation things would be much clearer I guess). Then t is optimised for an optimum k. Will going the straightforward way still be able to capture the effect I am trying to capture?

Sabrina Garland on 21 Jun 2024 at 16:43

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192736

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192736

Any suggestions @Torsten?

Torsten on 21 Jun 2024 at 17:44

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192816

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192816

Edited: Torsten on 21 Jun 2024 at 17:46

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 at 17:47

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192821

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192821

Okay! Thanks for suggestion @Torsten. Let me try this... But one last thing... Why does t(k) operation doesn't work with substitution? Is it because the equation becomes too complex?

Torsten on 21 Jun 2024 at 18:04

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192841

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192841

Edited: Torsten on 21 Jun 2024 at 18:06

Open in MATLAB Online

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)=

Defining variable as a function of other variable (11)

Sabrina Garland on 21 Jun 2024 at 18:14

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192876

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130756-defining-variable-as-a-function-of-other-variable#comment_3192876

Okay! Thanks @Torsten

Sign in to comment.

Defining variable as a function of other variable (2024)
Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 5731

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.