Step size increment?

조회 수: 13 (최근 30일)
Angela Mehta
Angela Mehta 2020년 7월 13일
댓글: Walter Roberson 2020년 7월 13일
Hello! I am new to Matlab and this might be a very simple question but please try and help me out!
So basically, I have both initial value(k0) and w(k0), where w is a function of k. Now we can step to k1 = k0 + dk and we will have to use w(k0) as the initial guess to find w(k1) then, k2 = k1 + dk and w(k2) can be found from w(k1) so on.
I have set k as the following;
k0 = 1/2;
w(k0) = 1.4;
k= linspace(1/2, 2/3, 101);
I would like find w(k).
Thank you very much!
  댓글 수: 2
Image Analyst
Image Analyst 2020년 7월 13일
What is the formula for w(k)? Or how does w(k+1) depend on w(k), the prior element?
Angela Mehta
Angela Mehta 2020년 7월 13일
편집: Angela Mehta 2020년 7월 13일
I have made a function that's dependent on w and k (myf2(w,k)) and I ran the function under a for loop. Here's part of the code that does that;
Nx= 201;
Ny= 301;
k0 = 1/3;
xa = 0;
xb = 5;,
ya = -3;
yb = 1;
%define x and y
x = linspace(xa, xb, Nx); %real part of w
y = linspace(ya, yb, Ny); % imag part of w
I = sqrt(-1);
for ix = 1:Nx
for iy = 1:Ny
z = x(ix) + I*y(iy);
func = @(z) myf2(z,k0);
funcp = @(z) myf2p(z,k0);
end
end
fprintf("Initial guess:\n");
[xi,yi] = ginput(1);
r0 = xi + I*yi;
r = r0;
maxit = 10; % max no of interations
%Runs Newton method
for it = 1:maxit
tol = 1e-16; %tolerance
r = newton(func, funcp, x0, tol, maxit);
format long
%fprintf("%12.12e \n", r); %12 digits
%disp(['f(x) = ' num2str(func(r))]); % residal
end
fprintf("Root: (%12.12e, %12.12e)\n", real(r), imag(r));
%%%%This is where I want to set w(k0) = real(r)
I hope that answered your question! Thanks a lot!

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 7월 13일
편집: Walter Roberson 2020년 7월 13일
k = linspace(1/2, 2/3, 101);
w = zeros(size(k));
w(k(1)) = 1.4;
for idx = 2 : numel(k)
w(idx) = function_to_find_w_k(k(idx), w(idx-1));
end
  댓글 수: 2
Angela Mehta
Angela Mehta 2020년 7월 13일
편집: Angela Mehta 2020년 7월 13일
Hello! I tried to use this code with my code, posted in the previous comment, I am getting an error when I set
w(k(1)) = real(r);
Error message: "Array indices must be positive integers or logical values."
I don't know the reason for it! Also apologies for setting w(k0)=1.4, I thought it might be easier that way.
Thank you very much!
Walter Roberson
Walter Roberson 2020년 7월 13일
k = linspace(1/2, 2/3, 101);
w = zeros(size(k));
w(1) = 1.4;
for idx = 2 : numel(k)
w(idx) = function_to_find_w_k(k(idx), w(idx-1));
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by