Invalid expression. Check for missing or extra characters

L = 3;
eta_0 = 1 % Initial value of eta
tol = 1e-5;
rho = 1;
max_iter = 100; % Maximum number of iterations
% Define the function Psi as a separate MATLAB function
function y = Psi(eta);
y = rho/2; % Implementation of the contraction mapping with coefficient rho = 1
end
% Step 2: Implement the iteration loop
iter = 1;
Function definitions in a script must appear at the end of the file.
Move all statements after the "Psi" function definition to before the first local function definition.
eta_n = eta_0;
delta_eta = [];
while iter <= max_iter
omega_n = 1 / (iter * (iter + 1));
zeta_n = 1 / (iter + 1);
vartheta_n = 1 / (iter + 1);
eta_n_plus_1 = vartheta_n * Psi((eta_n + eta_n_plus_1) / 2) + zeta_n * (eye(size(A)) - omega_n * A * J) * ((eta_n + eta_n_plus_1) / 2);
if norm(eta_n_plus_1 - eta_n) < tol
break;
end
delta_eta = [delta_eta, norm(eta_n_plus_1 - eta_n)];
eta_n = eta_n_plus_1;
iter = iter + 1;
end
% Step 4: Plot the convergence rate
figure;
plot(1:length(delta_eta), delta_eta);
xlabel('Iteration number');
ylabel('||eta_{n+1} - eta_n||');
% Step 5: Generate the table of number of iterations
iter_vec = 1:iter;
eta_vec = [eta_0, eta_n];
table_data = [iter_vec' eta_vec'];
% Step 6: Display the table and plot
disp('Table of number of iterations:');
disp(table_data);
% Plot the convergence rate graph

6 个评论

One obvious bug is the a function in the middle of a script rather than at the end:
What value do you expect eta_n_plus_1 to have on the first loop iteration? You refer to it before it has been defined.
rho is not passed as an input to the function psi()
Variable A and Variable J are not defined
rho = 1;
max_iter = 100; % Maximum number of iterations
% Define the function Psi as a separate MATLAB function
function y = Psi(eta);
y = rho/2; % Implementation of the contraction mapping with coefficient rho = 1
end
Your function takes a parameter named eta but ignores it and uses the value of rho which is not defined inside the function and is not passed as a parameter.
There are some cases in which a function can refer to variables that have been defined in another function -- but this only happens when a function is defined inside another function . You are defining a function inside a script rather than inside a function, and this kind of sharing is not permitted between scripts and functions.
You need to either pass rho to Psi, or else you need to use nested functions.
This is what i always encounter
Error: File: untitled5.m Line: 19 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "Psi" function definition to before the first local function definition.
@Stephen23 already told you what to do:
Move
% Define the function Psi as a separate MATLAB function
function y = Psi(eta);
y = rho/2; % Implementation of the contraction mapping with coefficient rho = 1
end
to the end of your script and define the unknown variable "rho" therein. Why don't you use "eta" if you supply it as input to the function ?

请先登录,再进行评论。

回答(1 个)

Doing what the error message says to do fixes that error... exposing other errors.
L = 3;
eta_0 = 1 % Initial value of eta
eta_0 = 1
tol = 1e-5;
rho = 1;
max_iter = 100; % Maximum number of iterations
% Step 2: Implement the iteration loop
iter = 1;
eta_n = eta_0;
delta_eta = [];
while iter <= max_iter
omega_n = 1 / (iter * (iter + 1));
zeta_n = 1 / (iter + 1);
vartheta_n = 1 / (iter + 1);
eta_n_plus_1 = vartheta_n * Psi((eta_n + eta_n_plus_1) / 2) + zeta_n * (eye(size(A)) - omega_n * A * J) * ((eta_n + eta_n_plus_1) / 2);
if norm(eta_n_plus_1 - eta_n) < tol
break;
end
delta_eta = [delta_eta, norm(eta_n_plus_1 - eta_n)];
eta_n = eta_n_plus_1;
iter = iter + 1;
end
Unrecognized function or variable 'eta_n_plus_1'.
% Step 4: Plot the convergence rate
figure;
plot(1:length(delta_eta), delta_eta);
xlabel('Iteration number');
ylabel('||eta_{n+1} - eta_n||');
% Step 5: Generate the table of number of iterations
iter_vec = 1:iter;
eta_vec = [eta_0, eta_n];
table_data = [iter_vec' eta_vec'];
% Step 6: Display the table and plot
disp('Table of number of iterations:');
disp(table_data);
% Plot the convergence rate graph
% Define the function Psi as a separate MATLAB function
function y = Psi(eta);
y = rho/2; % Implementation of the contraction mapping with coefficient rho = 1
end

2 个评论

Can you also check. I try all kind of means and this is what am facing
L = 3;
eta_0 = 1 % Initial value of eta
tol = 1e-5;
rho = 1;
max_iter = 100; % Maximum number of iterations
iter = 1:max_iter;
eta_n = eta_0;
delta_eta = [];
% Define the function Psi as a separate MATLAB function
function y = Psi(eta);
y = rho/2; % Implementation of the contraction mapping with coefficient rho = 1
end
% Step 2: Implement the iteration loop
while iter <= max_iter
Function definitions in a script must appear at the end of the file.
Move all statements after the "Psi" function definition to before the first local function definition.
omega_n = 1 / (iter * (iter + 1));
zeta_n = 1 / (iter + 1);
vartheta_n = 1 / (iter + 1);
eta_(n+1) = vartheta_n * Psi((eta_n + eta_(n+1)) / 2) + zeta_n * (eye(size(A)) - omega_n * A * J) * ((eta_n + eta_(n+1)) / 2);
if norm(eta_(n+1) - eta_n) < tol
break;
end
delta_eta = [delta_eta, norm(eta_(n+1) - eta_n)];
eta_n = eta_(n+1);
iter = iter + 1;
end
% Step 4: Plot the convergence rate
figure;
plot(1:length(delta_eta), delta_eta);
xlabel('Iteration number');
ylabel('||eta_{n+1} - eta_n||');
% Step 5: Generate the table of number of iterations
iter_vec = 1:iter;
eta_vec = [eta_0, eta_n];
table_data = [iter_vec' eta_vec'];
% Step 6: Display the table and plot
disp('Table of number of iterations:');
disp(table_data);
% Plot the convergence rate graph
If you have a specific reason for not moving the function definition like I showed, then you must use nested functions.
I also had to convert a number of * to .* and a number of / into ./ in order to be able to handle the fact that you are defining iter as a vector 1:max_iter . (I recommend firmly that you re-consider whether iter should be a vector or a scalar.)
draw_my_plots();
eta_0 = 1
Unrecognized function or variable 'eta_'.

Error in solution>draw_my_plots (line 31)
eta_(n+1) = vartheta_n * Psi((eta_n + eta_(n+1)) / 2) + zeta_n * (eye(size(A)) - omega_n * A * J) * ((eta_n + eta_(n+1)) / 2);
%an outer function is needed in order to be permitted to define a nested
%function. A nested function is needed in order to be able to access the
%rho variable inside Psi
function draw_my_plots
L = 3;
eta_0 = 1 % Initial value of eta
tol = 1e-5;
rho = 1;
max_iter = 100; % Maximum number of iterations
iter = 1:max_iter;
eta_n = eta_0;
delta_eta = [];
% Define the function Psi as a separate MATLAB function
function y = Psi(eta);
y = rho/2; % Implementation of the contraction mapping with coefficient rho = 1
end
% Step 2: Implement the iteration loop
while iter <= max_iter
omega_n = 1 ./ (iter .* (iter + 1));
zeta_n = 1 ./ (iter + 1);
vartheta_n = 1 ./ (iter + 1);
eta_(n+1) = vartheta_n * Psi((eta_n + eta_(n+1)) / 2) + zeta_n * (eye(size(A)) - omega_n * A * J) * ((eta_n + eta_(n+1)) / 2);
if norm(eta_(n+1) - eta_n) < tol
break;
end
delta_eta = [delta_eta, norm(eta_(n+1) - eta_n)];
eta_n = eta_(n+1);
iter = iter + 1;
end
% Step 4: Plot the convergence rate
figure;
plot(1:length(delta_eta), delta_eta);
xlabel('Iteration number');
ylabel('||eta_{n+1} - eta_n||');
% Step 5: Generate the table of number of iterations
iter_vec = 1:iter;
eta_vec = [eta_0, eta_n];
table_data = [iter_vec' eta_vec'];
% Step 6: Display the table and plot
disp('Table of number of iterations:');
disp(table_data);
% Plot the convergence rate graph
end

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by