About Newton's method

6 visualizaciones (últimos 30 días)
Zekun Xue
Zekun Xue el 15 de Feb. de 2020
Comentada: Zekun Xue el 15 de Feb. de 2020
Hello guys,
thanks for looking over here!
I'd like to ask about Newton's method in the MATLAB, I have seen many questions about the newton's method in the forum, while I have tried to apply those in my code but it seems that something goes wrong and I spent a lot of time trying to figure what's going on. HereI attached my code, I appreicate if you can help me to figure it out.
Thanks in advance again!
This is what I have wrote:
clear all
close all
u0 = inf; % x0 initial point
h1 = 1; % constant in the equation
E = 25; % constant in the equation
Number = 10; %N
tol = 1e-10;
u(1) = u0;
n = 2;
nfinal = Number+1;
Dm = 10; % constant in the equation
N = 10; % constant in the equation
P1 = (E - (abs(h1).^2).* (exp(N./Dm)-1).* u.^-1)./(Dm + u.^-1);
P2 = (E + (abs(h1).^2).* (exp(N./Dm)-1).* Dm)./(Dm + u.^-1);
A = log2(1+(abs(h1).^2).*P2);
B = N - Dm.* log2(1+exp(-N./Dm).*P1.*abs(h1).^2);
F = A - u.*B; %% equation of the function
while(n<= Number + 1)
Fe = F(u(n-1));
Be = B(u(n-1));
u(n) = u(n-1) + Fe./(Be-(E+Dm.*(exp(N/Dm)-1))/(exp(N/Dm).*Dm.*u+E.*u + 1));
Tn(n) = 1/u(n);
if(abs(Fe)<= tol)
nfinal = n;
break;
end
n = n+1;
end
delay_minimize = Dm + Tn;
plot(0:nfinal-1,delay_minimize)
%%end of the code
What goes wrong is that in the line:
Fe = F(u(n-1));
The MATLAB gives me that the index has to be positve integer or logical value. But I already set n to the 2, I don't really know what's going on.
Btw, the reason for I didn't use diff function is that the F'(u) is equal to the equation I put in the while loop which is : (Be-(E+Dm.*(exp(N/Dm)-1))/(exp(N/Dm).*Dm.*u+E.*u + 1)); and the requirement of the the initial point has to be inf.
Thank you! have a good day!

Respuestas (1)

Steven Lord
Steven Lord el 15 de Feb. de 2020
As you've written your code F isn't a function. It's a numeric array. That means F(u(n-1)) is an attempt to get whatever is in element u(n-1) of F.
You probably want to specify your function as an anonymous function (if it's simple enough) or as a function handle to a function written in a file (if it's more complicated.) Search the documentation for "anonymous function" or "function handle" for more information.
F = @(u) someExpressionDependingOnTheVariableu
You will need to fill in what "someExpressionDependingOnTheVariableu" is. You'll also need to do the same thing for B.
  1 comentario
Zekun Xue
Zekun Xue el 15 de Feb. de 2020
Thanks for your reply!
I tried to learn the documentations you referred, and now I can understand what you were saying.
May I ask you one more question?
I used the diff function instead now, but MATLAB gives me :Assignment cannot be performed because the number of elements on the left and right is different(which appear in the line of: u(n) = u(n-1) - Fe./Fpe)
The code I have changed:
F = @(u)A-u.*B; %% equation of the function
Fp = @(u) diff(F(u(n-1)))
while(n<= Number + 1)
Fe = F(u(n-1));
Fpe = Fp(u(n-1));
u(n) = u(n-1) - Fe./Fpe;
if(abs(Fe)<= tol)
nfinal = n;
break;
end
n = n+1;
end

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by