Parse error at x

6 visualizzazioni (ultimi 30 giorni)
Abul Yasa Hasan
Abul Yasa Hasan il 2 Apr 2024
Risposto: Steven Lord il 2 Apr 2024
I was trying to solve the question attacjed and was writing the code for it. But I keep getting the parse error. I went through instances where poeple had the same issue but every other one seems to be different. (Line 10)
% Define the objective function
function f = objective_function(x)
f1 = x(1) ^ 3 + x(1) ^ 2 * x(2) - x(1) * x(3) + 6;
f2 = exp(x(1)) + exp(x(2)) - x(3);
f3 = x(2) ^ 2 - 2 * x(1) * x(3) - 4;
f = f1 ^ 2 + f2 ^ 2 + f3 ^ 2;
end
% Initial guess for x
x = [0.5; 0.5; 0.5];
% Tolerance value
TOL = 0.05;
% Perform steepest descent
solution = steepest_descent(x_initial, TOL);
fprintf('Approximate solution: [%f, %f, %f]\n', solution);
fprintf('Objective function value at solution: %f\n', objective_function(solution));
% Define the gradient of the objective function
function df = gradient(x)
df_dx1 = 2 * (x(1) ^ 3 + x(1) ^ 2 * x(2) - x(1) * x(3) + 6) * (3 * x(1) ^ 2 + 2 * x(1) * x(2) - x(3)) ...
+ 2 * (exp(x(1)) + exp(x(2)) - x(3)) * exp(x(1)) ...
+ 2 * (x(2) ^ 2 - 2 * x(1) * x(3) - 4) * (-2 * x(3));
df_dx2 = 2 * (x(1) ^ 3 + x(1) ^ 2 * x(2) - x(1) * x(3) + 6) * (x(1) ^ 2) ...
+ 2 * (exp(x(1)) + exp(x(2)) - x(3)) * exp(x(2)) ...
+ 2 * (x(2) ^ 2 - 2 * x(1) * x(3) - 4) * (2 * x(2));
df_dx3 = -2 * (x(1) ^ 3 + x(1) ^ 2 * x(2) - x(1) * x(3) + 6) * x(1) ...
+ 2 * (exp(x(1)) + exp(x(2)) - x(3)) ...
+ 2 * (x(2) ^ 2 - 2 * x(1) * x(3) - 4) * (-2 * x(1));
df = [df_dx1; df_dx2; df_dx3];
end
% Steepest descent algorithm
function solution = steepest_descent(x_initial, tolerance)
x_current = x_initial;
while true
gradient_current = gradient(x_current);
if norm(gradient_current) < tolerance
break;
end
% Choose a step size (you may need to adjust this)
alpha = 0.01;
x_next = x_current - alpha * gradient_current;
x_current = x_next;
end
solution = x_current;
end

Risposte (2)

Steven Lord
Steven Lord il 2 Apr 2024
If all of the code you posted is in the same file, the line where you define the initial value occurs after the end keyword that ends your function. If you were to try to run that function, that line of code cannot be executed. MATLAB doesn't understand what you're trying to do with that code and so it would error.
Either move the code that follows the definition of the function to a separate file or move the function definition to the end (or if you're using release R2024a, to anywhere but the start) of the file. Since you've already got other functions defined after the commands that call your steepest_descent function, moving objective_function to the end of the file would probably be the easiest.

Chuguang Pan
Chuguang Pan il 2 Apr 2024
The error message shows that there is a unrecognized varialble x_initial. You should initialize it.
x_initial=[.5;.5;.5];

Categorie

Scopri di più su Scope Variables and Generate Names in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by