Invalid expression. Check for missing or extra characters

2 Ansichten (letzte 30 Tage)
John
John am 6 Sep. 2023
Kommentiert: Walter Roberson am 7 Sep. 2023
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 Kommentare
John
John am 6 Sep. 2023
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.
Torsten
Torsten am 6 Sep. 2023
Bearbeitet: Torsten am 6 Sep. 2023
@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 ?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 6 Sep. 2023
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 Kommentare
John
John am 7 Sep. 2023
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
Walter Roberson
Walter Roberson am 7 Sep. 2023
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by