Step size increment?

12 views (last 30 days)
Angela Mehta
Angela Mehta on 13 Jul 2020
Commented: Walter Roberson on 13 Jul 2020
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 Comments
Image Analyst
Image Analyst on 13 Jul 2020
What is the formula for w(k)? Or how does w(k+1) depend on w(k), the prior element?
Angela Mehta
Angela Mehta on 13 Jul 2020
Edited: Angela Mehta on 13 Jul 2020
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!

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Jul 2020
Edited: Walter Roberson on 13 Jul 2020
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 Comments
Angela Mehta
Angela Mehta on 13 Jul 2020
Edited: Angela Mehta on 13 Jul 2020
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 on 13 Jul 2020
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

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!