Help implementing code (Newton-Raphson)

2 次查看(过去 30 天)
%% 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 个评论
Dyuman Joshi
Dyuman Joshi 2023-2-22
A small correction, the notation for power is
%10^k is represent by 1ek
val=10^(-14)
val = 1.0000e-14
%This is incorrect
maxTol=10e-14
maxTol = 1.0000e-13
%it should be
maxTol=1e-14
maxTol = 1.0000e-14
Askic V
Askic V 2023-2-22
In addition to what @Dyuman Joshi mentioned, the same goes for Is. 10^(-12) is 1e-12 in Matlab.

请先登录,再进行评论。

采纳的回答

Askic V
Askic V 2023-2-22
编辑:Askic V 2023-2-22
@Allan I would like to suggest somewhat simpler code:
maxIter = 200;
maxTol = 1e-14;
V0 = linspace(0, 10, maxIter);
% parameters
R = 10;
Is = 1e-12;
alpha = 40;
params = [R, Is, alpha];
x0 = [1;0]; % initial conditions
xp = x0;
% each column contains x(1), x(2) and V0 for each of 200 values
answers_matrix = zeros(3,200);
for i = 1:numel(V0) % maxIter
while true
[F, JF] = non_lin_P3(xp, V0(i), params);
if norm(F) < maxTol
break;
end
xn = xp - JF\F;
% update values for next iteration
xp = xn;
end
answers_matrix(:,i) = [xn; V0(i)];
end
function [F, J] = non_lin_P3(x,V0, params) %x is a vector
% extract parameters
R = params(1);
Is = params(2);
alpha = params(3);
% extract elements of x
x1 = x(1); % variable i
x2 = x(2); % variable vd
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)];
end
This code will use for each new value of voltage V0, previous solution (xp) as initial guess.
The execution will also terminate if magnitude is less than 10^(-14), since this is the only stop criteria defined in your task.
  4 个评论
Askic V
Askic V 2023-2-22
@Dyuman Joshi yes, it could be implemented in that way, but not necessarily. i wrote in the comment that each column consists of x(1), x(2) and V0(i) which implies that the last element in the column is V0. One only needs to keep that in mind.
@Allan depends on what you need to do. The code looks correct and executes correctly. I'm not sure why that would be of interest. Do you have that described in the second part of the task?
Allan
Allan 2023-2-22
The task is simply to plot in subplots.Im sure it is to allow my peers and I to utilize the subplot function.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by