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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by