How to apply looping in iteration process ?

Hi everyone,
I required to apply diffrent initail guess for iterative solutions. For a single solution, the following script works perfectly.
L = readmatrix('R_bounds_0.01.csv'); (coloumn matrix with 1986 rows)
for i=1:length(L);
f = @(n)((n+1)*log(n+1)-n)/n^2 - L(i);
n0 = 100; % Initial guess
n(i) = fzero(f,n0 );
disp(n );
end
nn=n';
csvwrite('N_lower_100.csv',nn);
However, i need to try with different values of initial guesses [0, 10, 100, 1000] etc. Someone suggets me a modified script but that is still not working,
A = readmatrix('R_mean_value_0.01.csv');
m = length(A) ;
IG = [0, 10, 100, 1000] ; % initial guess
n = length(IG) ;
nn = zeros(m,n) ;
for i = 1:n
n0 = IG(i) ;
for j=1:m
f = @(n)((n+1)*log(n+1)-n)/n^2 - A(j);
nn(i,j) = fzero(f,n0 );
disp(nn(i,j) );
end
end
csvwrite('N_mean_10000.csv',nn);
May someone suggest me any possibel solution.
Thank you!

6 件のコメント

Mathieu NOE
Mathieu NOE 2022 年 3 月 17 日
hello
can you share the csv file as well (R_mean_value_0.01.csv)
Andi
Andi 2022 年 3 月 17 日
Sure, Attached with main question ..
Torsten
Torsten 2022 年 3 月 17 日
Someone suggets me a modified script but that is still not working,
Do you mean that you get a syntax error message or that fzero has problems finding a solution ?
Andi
Andi 2022 年 3 月 17 日
There is no syntax error, but fzero problem.
Torsten
Torsten 2022 年 3 月 17 日
The implementation is correct.
I think the task of the assignment was to show you that the rootfinder may fail if the initial guess is far off. So there is nothing wrong with your code if fzero does not converge in all test cases.
Andi
Andi 2022 年 3 月 18 日
I agreed, sometime the initial guess is a bit far from the solution that why i used different initial guess. But that not works.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 3 月 17 日

0 投票

If your initial guess is not between about -1 and about +40 then you might not get a solution.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/930814/R_mean_value_0.01%20.csv';
A = readmatrix(filename);
syms N
f = @(n)((n+1)*log(n+1)-n)/n^2;
baseeqn = f(N);
fplot(baseeqn, [-1.5 40])
hold on
minA = min(A)
minA = 0.0706
maxA = max(A)
maxA = 0.9681
yline([minA, maxA]);
ylim([minA-1/4, maxA+1/4])
hold off
sol1 = vpasolve(baseeqn - A(1), 0.5)
sol1 = 
15.859241089220361924912561179785
fplot(baseeqn, [32 40]); hold on;
yline([minA]);
hold off

6 件のコメント

Andi
Andi 2022 年 3 月 18 日
Despite of chaniging the initial guess, still got same error
A = readmatrix('R_mean_value_0.01.csv');
m = length(A) ;
IG = [-1.5, 1, 10, 40] ; % initial guess
n = length(IG) ;
nn = zeros(m,n) ;
for i = 1:n
n0 = IG(i) ;
for j=1:m
f = @(n)((n+1)*log(n+1)-n)/n^2 - A(j);
nn(i,j) = fzero(f,n0 );
disp(nn(i,j) );
end
end
% Error
Error using fzero
Initial function value must be finite and real.
Error in untitled (line 10)
nn(i,j) = fzero(f,n0 );
I think initial guss cant be negative, therefore i used [0, 1, 10, 100] as an initial guess.
Walter Roberson
Walter Roberson 2022 年 3 月 18 日
You use log(n+1) . n+1 would be negative for n < -1 and that would give you a complex log. So your n values can never be <= -1 .
You can use a vector of two values for the second parameter to fzero() to prevent fzero from guessing that a number < -1 might perhaps be acceptable.
Andi
Andi 2022 年 3 月 18 日
The following script works but i unable to process teh output i dont knwon why (e.g. unable to write in a csv file)
A = readmatrix('R_mean_value_0.01.csv');
m = length(A) ;
IG = [0.0001,0.001, 0.01, 0.1, 1, 10, 100, 1000] ; % initial guess
n = length(IG) ;
nn = zeros(8,n) ;
for i = 1:n
n0 = IG(i) ;
for j=1:m
f = @(n)((n+1)*log(n+1)-n)/n^2 - A(j);
nn(i,j) = fzero(f,n0 );
disp(nn(i,j) );
end
end
%SS=nn';
csvwrite('NNNN.csv',nn);
%TTT = (SS(:,1), SS(:,2), SS(:,3), SS(:,4), SS(:,5), SS(:,6), SS(:,7), SS(:,8));
Walter Roberson
Walter Roberson 2022 年 3 月 18 日
As an outsider.... I do not know that your current directory is one that you have write access to.
Andi
Andi 2022 年 3 月 18 日
If i run the same scipt using one initail guess, i can write the output in csv but when use for loop then i cant. This seems that tehre is no issue of directory.
Mathieu NOE
Mathieu NOE 2022 年 3 月 18 日
hello
a very minor bug in the first code posted is the initialization of nn is done with the wrong dimensions (flip m and n)
I have no problel running the corected code - attached the output file
of course the IG must contains only values strictly above -1 (otherwise log(n+1) is complex) and different from zero (division by zero)
A = readmatrix('R_mean_value_0.01.csv');
m = length(A) ;
IG = [0.0001,0.001, 0.01, 0.1, 1, 10, 100, 1000] ; % initial guess (n> - 1) and (n # 0)
n = length(IG) ;
nn = zeros(n,m) ; % mod here
for i = 1:n
n0 = IG(i) ;
for j=1:m
f = @(n)((n+1)*log(n+1)-n)/n^2 - A(j);
nn(i,j) = fzero(f,n0 );
disp(nn(i,j) );
end
end
csvwrite('N_mean_10000.csv',nn);

サインインしてコメントする。

カテゴリ

製品

タグ

質問済み:

2022 年 3 月 17 日

コメント済み:

2022 年 3 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by