How can I vectorize my code and use fsolve to solve thousands of non-linear equations simulataneously?
Show older comments
Dear Sir/Madam,
Please see the following piece of code: %%%%%%%%%%
M=4; lamda_lin=0.1;
sr_vect=0:729; rp_vect=0:729; rd_vect=0:729;
beta_opt_matrix = generate_beta_opt_grid(sr_vect,rp_vect,rd_vect,lamda_lin,M);
function beta_opt_matrix = generate_beta_opt_grid(sr_vect,rp_vect,rd_vect,lamda_lin,M)
beta_opt_matrix = zeros(length(sr_vect),length(rp_vect),length(rd_vect));% initialization of matrix
%gereration of 3D data space and finding \beta_{opt}
for i = 1:length(sr_vect) for j = 1:length(rp_vect) for k= 1:length(rd_vect) beta_opt_matrix(i,j,k) = find_beta_opt(sr_vect(i),rp_vect(j),rd_vect(k),lamda_lin,M); end end end
%%%
I use find_beta_opt function passing scalars to a non-liner equation and solving it. This is taking lot of time.
how can I vectorize the above code and use fsolve to solve for 729*729*729 grid points simultaneously.
Kindly reply, Thanks very much in advance, sainath.
Answers (1)
What does a long time mean? You realize you are trying to solve almost 400 million systems of non-linear equations. That is bound to take a while. You could try parallelization.
doc parfor
For instance:
beta_opt_matrix = NaN * ones(730,730,730);
parfor ii=1:730^3;
[sr rp rd] = ind2sub([730 730 730],ii);
beta_opt_matrix(sr,rp,rd) = find_beta_opt(sr-1, rp-1, rd-1, lambda, M);
end
Categories
Find more on Linear Algebra 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!