Parse error at x

조회 수: 6 (최근 30일)
Abul Yasa Hasan
Abul Yasa Hasan 2024년 4월 2일
답변: Steven Lord 2024년 4월 2일
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

답변 (2개)

Steven Lord
Steven Lord 2024년 4월 2일
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 2024년 4월 2일
The error message shows that there is a unrecognized varialble x_initial. You should initialize it.
x_initial=[.5;.5;.5];
  댓글 수: 1
Abul Yasa Hasan
Abul Yasa Hasan 2024년 4월 2일
It's still the same.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by