Borrar filtros
Borrar filtros

Operator '*' is not supported for operands of type 'function_handle'.

4 visualizaciones (últimos 30 días)
draw_my_plots
eta_0 = 1
Operator '*' is not supported for operands of type 'function_handle'.

Error in solution>draw_my_plots (line 25)
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);
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
n = 1:max_iter;
eta_n = eta_0;
eta_(n+1) = (eta_n) + 1;
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 n <= max_iter
omega_n = 1 ./ (n .* (n + 1));
zeta_n = 1 ./ (n + 1);
vartheta_n = 1 ./ (n + 1);
A = @(eta) L * eta(n) ;
J = @(eta) eta(n);
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);
n = n + 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
n_vec = 1:iter;
eta_vec = [eta_0, eta_n];
table_data = [iter_vec' n_vec'];
% Step 6: Display the table and plot
disp('Table of number of iterations:');
disp(table_data);
% Plot the convergence rate graph
end

Respuesta aceptada

John D'Errico
John D'Errico el 7 de Sept. de 2023
Editada: John D'Errico el 7 de Sept. de 2023
Too many errors in this code to list them all out. Starting at the beginning...
eta_0 = 1 % Initial value of eta
That line does not define a vector, with the zero'th element as 1. It just creates a scalar variable, with name eta_0.
And worse, MATLAB has an index origin at 1, not 0 anyway.
Later on, we see these lines:
n = 1:max_iter;
eta_n = eta_0;
eta_(n+1) = (eta_n) + 1;
Again, this is not valid MATLAB syntax, to index into a vector like that using unerscores. You do it frequently later on.
Other things you do wrongly...
n = 1:max_iter;
...
But then we see a while loop like this:
while n <= max_iter
n is a vector. It was initialized as such. This is not how a while loop works. As it is you do increment n in side the loop, but n is a VECTOR. Adding 1 to a vector just adds 1 to every element of that vector.
Then I see this defined in the middle of your code:
function y = Psi(eta);
y = rho/2; % Implementation of the contraction mapping with coefficient rho = 1
end
Psi has eta as an argument, but you never use eta, only rho.
The result will be many strange errors, as MATLAB tries to interpret what you are writing as MATLAB syntax, when it is not valid at all.
I would strongly recommend you actually learn MATLAB, by starting with the MATLAB onramp tutorial, or something like it. As for how the code should be written? I have no idea, since I cannot even guess what it is you are really trying to do from a pile of invalid syntax.
  1 comentario
Stephen23
Stephen23 el 7 de Sept. de 2023
Editada: Stephen23 el 7 de Sept. de 2023
E.g. there are free tutorials here:
Because learning how to write MATLAB code is more reliable than guessing and making things up.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by