Help implementing code (Newton-Raphson)
Afficher commentaires plus anciens
%% I need help implementing the hint listed below into my script/function and verifying if this is the solution too the problem (a). It says to use the previous solution of i and vd as the intial guess. Since we were never given the intial guess, I just put 0 and 0. I have included the function "non_lin_3" at the bottom. I'm not sure if this is right, but I have inlcuded the non-linear equations and the question:


%%
%% Part A
maxIter=200;
maxTol=10e-14;
V0 = linspace(0,10,200);
answers_matrix = zeros(3,200);
xj = [0;0];
for j = 1:maxIter
V_j = V0(j);
answers_matrix(1,j) = V0(j); %sets first row at jth spot to V0
%use NR method to find the root for the given value of V_j
for nInters_a=1: maxIter
[Fj,Jj] = non_lin_P3(xj,V_j);
if norm(Fj) < maxTol
xy= xj;
fRoot = Fj;
answers_matrix([2 3],j)=xj;%appends roots for given V0 value to answers_matrix at the jth column
break
end
xj = xj -Jj\Fj;
end
xj = xy; % sets old solution as new guesses.
end
========================================================================
function [F, J] = non_lin_P3(x,V0) %x is a vector
%extract elements of x
R = 10;
Is = 10e-12;
alpha = 40;
x1 = x(1);
x2 = x(2);
F1 = R*x1+x2-V0;
F2 = x1-Is*exp(alpha*x2)-Is*1;
F = [F1; F2];
J = [R 1; 1 -1*Is*alpha*exp(alpha.*x2)];
2 commentaires
A small correction, the notation for power is
%10^k is represent by 1ek
val=10^(-14)
%This is incorrect
maxTol=10e-14
%it should be
maxTol=1e-14
Askic V
le 22 Fév 2023
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Newton-Raphson Method dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!