# Newton Raphson - saving all values and using last iteration value as initial for next

10 visualizaciones (últimos 30 días)
HMZ el 19 de Abr. de 2021
Comentada: HMZ el 20 de Abr. de 2021
I'm attempting to do a Newton - Raphson calculation but am having trouble starting. A1 and B1 are both matrices [1x15], so for the the values of A1, B1 and Theta_F, I need to perform a Newton - Raphson calculation over 5 iterations, saving all iteration values (for plotting) and using the last iteration value as the initial guess for the next N-R step (with the next set of A1, B1 and Theta_F values)
I'm really not sure where to start or how best to approach it (not been using MATLAB very long), any help would be greatly appreciated!
Many thanks.
M_s = 0; % Other variables in equations
alpha_s = 0;
Theta_F = [1:1:15]
A1 = [0,-30,-120,-270,-480,-750,-1080,-1470,-1920,-2430,-3000,-3630,-4320,-5070,-5880]
% B1 has the same value but is calculated as a matrix
B1 = [-196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200]
f = @(x) A1.*cos(x) + B1.*sin(x) + M_s*(x + (Theta_F ./(180*pi)) + (alpha_s /(180*pi))); % Function
fd = @(x) -A1.*sin(x) + B1.*cos(x) - M_s; % Function derivative
x0 = 0.0001;
x = x0;
% Attempt at N-R
for i = 1:5
x(i+1) = x(i) - f(x(i))/fd(x(i))
i = i+1
end
##### 7 comentariosMostrar 5 comentarios más antiguosOcultar 5 comentarios más antiguos
Andrew Newell el 19 de Abr. de 2021
Editada: Andrew Newell el 20 de Abr. de 2021
Actually, for and equal to zero, the solution is . Are you going to need to do this for nonzero parameters? If not, you're done.
HMZ el 19 de Abr. de 2021
Yes, I've created a spreadsheet to practice the mathematics behind the process and ensure the values are sensible. The values of x (for corresponding value of A1, B1 and Theta_f) after 5 iterations come out to be;
x = [0
-0.000152905
-0.000611621
-0.001376146
-0.002446478
-0.003822611
-0.005504532
-0.007492215
-0.00978562
-0.012384688
-0.015289328
-0.018499418
-0.022014791
-0.025835229
-0.029960451
]

Iniciar sesión para comentar.

Andrew Newell el 20 de Abr. de 2021
Editada: Andrew Newell el 20 de Abr. de 2021
Sorry, I just realized that you wanted to save the iterations. Here's how:
x = -0.001*ones(6,length(A1));
for i=1:5
x(i+1,:) = x(i,:) - f(x(i,:))./fd(x(i,:));
end
The top row of this matrix has the initial guess and the next five rows have the five iterations.
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
HMZ el 20 de Abr. de 2021
Thank you so much for you help! It is extremely appreciated

Iniciar sesión para comentar.

### Más respuestas (2)

Paul Hoffrichter el 20 de Abr. de 2021
Editada: Paul Hoffrichter el 20 de Abr. de 2021
The final x values match your spreadsheet results. Your suggested x0 converges too soon to be interesting. Set it to 1.0 to actually see convergence in action.
format long
M_s = 0; % Other variables in equations
alpha_s = 0;
Theta_F = [1:1:15]
iMax = 5;
A1 = [0,-30,-120,-270,-480,-750,-1080,-1470,-1920,-2430,-3000,-3630,-4320,-5070,-5880]
% B1 has the same value but is calculated as a matrix
B1 = [-196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200, -196200]
f = @(x) A1.*cos(x) + B1.*sin(x) + M_s*(x + (Theta_F ./(180*pi)) + (alpha_s /(180*pi))); % Function
fd = @(x) -A1.*sin(x) + B1.*cos(x) - M_s; % Function derivative
x0 = 0.0001;
% x0 = 1; % more interesting starting point
x = zeros(1, length(A1));
x(:) = x0;
xSave = zeros(iMax, length(A1));
% Attempt at N-R
for i = 1:iMax
x = x - f(x)./fd(x);
xSave(i,:) = x;
i = i+1;
end
% Check;
y = f(x)
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
HMZ el 20 de Abr. de 2021
This also worked, so thank you very much as well for all your help!

Iniciar sesión para comentar.

Andrew Newell el 20 de Abr. de 2021
O.k. So here is how you do it:
x = -0.001*ones(size(A1));
for i=1:5
x = x - f(x)./fd(x);
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Newton-Raphson Method en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by