Help revise the code

26 views (last 30 days)
Jiiim Wu
Jiiim Wu on 22 Aug 2023
Commented: Walter Roberson on 22 Aug 2023
% Given constants
q = 0.2;
alpha = 0.7;
gamma = 0.1;
K_bar = 1;
z_hat = 1.1;
eta = 0.2;
% Define the range of epsilon values
epsilon_range = 0.1:0.01:1;
% Preallocate array to store z_star values
z_star_values = zeros(size(epsilon_range));
% Define the options for fsolve
options = optimoptions('fsolve', 'Display', 'off', 'TolFun', 1e-6);
for i = 1:length(epsilon_range)
epsilon_val = epsilon_range(i);
% Define the equation for fsolve
equation = @(z_star) shooting_equation(z_star, epsilon_val, q, alpha, gamma, K_bar, z_hat, eta);
% Solve for z_star using fsolve
z_star_initial_guess = 0.5; % Initial guess for z_star
z_star_values(i) = fsolve(equation, z_star_initial_guess, options);
end
% Plot z_star as a function of epsilon
figure;
plot(epsilon_range, z_star_values, 'b', 'LineWidth', 2);
xlabel('\epsilon');
ylabel('z^*');
title('Plot of z^* as a Function of \epsilon');
grid on;
function value = shooting_equation(z_star, epsilon, q, alpha, gamma, K_bar, z_hat, eta)
value = z_star - z_star * (((1 - alpha) * epsilon) / (1 - alpha * epsilon))^((1 - epsilon) / epsilon) ...
- q^((1 - epsilon) / epsilon) * (K_bar)^((epsilon - 1) / epsilon) ...
* (1)^((epsilon - 1) / (alpha * epsilon)) * ((1 - alpha) / alpha)^((1 - alpha) * (epsilon - 1) / (alpha * epsilon)) ...
* (epsilon)^(((1 - epsilon + alpha * epsilon) * (1 - epsilon)) / (alpha * epsilon^2)) ...
* ((alpha) / (1 - alpha * epsilon))^((1 - epsilon) / epsilon) ...
* (alpha * epsilon)^((epsilon - 1) / (alpha * epsilon^2)) ...
* (((1 - epsilon) / (0 - 2))^((1 - epsilon) * (epsilon - 1 + alpha * epsilon) / (alpha * epsilon^2))) ...
* ((((2)^(1 / (1 - epsilon)) - (z_hat + eta)^(1 / (1 - epsilon))) + (((z_hat - eta)^(1 / (1 - epsilon))) - (z_star)^(1 / (1 - epsilon))) ...
+ ((1 - gamma)^(epsilon / (1 - epsilon))) * (((z_hat + eta)^(1 / (1 - epsilon))) - ((z_hat - eta)^(1 / (1 - epsilon))))) ...
^((1 - epsilon) * (epsilon - 1 + alpha * epsilon) / (alpha * epsilon^2)) - z_star;
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
end

Answers (1)

Walter Roberson
Walter Roberson on 22 Aug 2023
starting from the beginning of
* ((((2)^(1 / (1 - epsilon)) - (z_hat + eta)^(1 / (1 - epsilon))) + (((z_hat - eta)^(1 / (1 - epsilon))) - (z_star)^(1 / (1 - epsilon))) ...
you have one extra ( that does not have a matching )
value = z_star - z_star * (((1 - alpha) * epsilon) / (1 - alpha * epsilon))^((1 - epsilon) / epsilon) - q^((1 - epsilon) / epsilon) * (K_bar)^((epsilon - 1) / epsilon) * (1)^((epsilon - 1) / (alpha * epsilon)) * ((1 - alpha) / alpha)^((1 - alpha) * (epsilon - 1) / (alpha * epsilon)) * (epsilon)^(((1 - epsilon + alpha * epsilon) * (1 - epsilon)) / (alpha * epsilon^2)) * ((alpha) / (1 - alpha * epsilon))^((1 - epsilon) / epsilon) * (alpha * epsilon)^((epsilon - 1) / (alpha * epsilon^2)) * (((1 - epsilon) / (0 - 2))^((1 - epsilon) * (epsilon - 1 + alpha * epsilon) / (alpha * epsilon^2))) * ((((2)^(1 / (1 - epsilon)) - (z_hat + eta)^(1 / (1 - epsilon))) + (((z_hat - eta)^(1 / (1 - epsilon))) - (z_star)^(1 / (1 - epsilon))) + ((1 - gamma)^(epsilon / (1 - epsilon))) * (((z_hat + eta)^(1 / (1 - epsilon))) - ((z_hat - eta)^(1 / (1 - epsilon)))))^((1 - epsilon) * (epsilon - 1 + alpha * epsilon) / (alpha * epsilon^2)) - z_star;
% 123 2 1 2 10 12 1 0 12 1 0 1 0 12 1 0 1 0 12 1 2 10 12 1 0 12 1 2 1 2 10 1 0 123 2 3 21 2 10 12 1 2 10 12 1 0 1 0 12 1 2 10 123 2 3 21 23 2 3 2 3 210 1234 3 4 5 43 4 3 4 5 432 345 4 5 6 543 4 3 4 5 432 34 3 4 5 432 345 4 5 6 543 45 4 5 6 54321 23 2 3 2 3 21
The number under each bracket is the number of open brackets in effect "after" the character above the number.
  2 Comments
Jiiim Wu
Jiiim Wu on 22 Aug 2023
Moved: Bruno Luong on 22 Aug 2023
Would it be possible that you can provide the correct code. It looks still confusing to me. Thanks
Walter Roberson
Walter Roberson on 22 Aug 2023
No, I cannot do that, as I do not know what the correct equations are.
I highly recommend that you break the function up into multiple statements, instead of using one long long line of code.
That would also allow you to do optimizations such as calculating alpha * epsilon and 1-epsilon into variables so that those expressions do not need to be calculated multiple times.
Using a long line of source is harder for humans to read, and harder for MATLAB to analyze the flow of. And tends to involve calculating the same sub-expression multiple times unnecessarily.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!