Fast way to solve one nonlinear equation with large data parameters?
3 views (last 30 days)
Show older comments
I am trying to solve a non-linear equation. For example:

I want to solve for n12 on the left side and I set the parameters n1 and n2 constant. Now I do not know to solve with different phi2 values efficiently. I need to get n12 for 200,000,000 phi2 points. I wonder what the fastest way to do this is?
I think I can do some algebra and isolate n12 because the right hand side essentially becomes just a number. Then n12 can be solved by putting different "constants" of the right hand side. Doing it by first isolating n12 and then plugging numbers into it would use a for loop for the 200 million points.
I was also thinking of using fsolve or fzero but I do not know how to put phi2 in many times without using a for loop. This method gets slower the more phi2's there are. (fun = equation above all moved to one side to have everything equal 0) I have:
n12 = [];
for i = 1:length(phi2)
equat = @(n)fun(n12,n1,n2,phi2(i));
sol = fsolve(equat,n1);
n12 = [n12;sol];
end
fzero would use the same code essentially so I don't know if it is faster
What is the fastest way to solve for n12 having a large data set?
1 Comment
TUNAHAN YILMAZ
on 10 Mar 2021
I have similar problem. I use vpasolve to find 1 equation 1 unknown, highly nonlinear, such as;
%% Defining some constants
th_spr_free = pi/2;
%%
th_spr = sym('th_spr',100); %Define th_spr symbolically (1x100)
assume(th_spr>pi/6 & th_spr<5*pi/6);
N = length(th_spr) % N = 100
th_spr_found = zeros(1,N);
T_spr_symbolic = -K *(th_spr-th_spr_free); % Nmm
for k=1:N
eqnLeft = T_spr_symbolic(k); %Linear equation dependent on th_spr
function_to_find_j4; %A function to find j4 in terms of th_spr
eqnRight = -(1000)*j4; %Highly nonlinear equation dependent on th_spr
S = vpasolve(eqnLeft == eqnRight, th_spr(k),[pi/6 5*pi/6]); % vpasolve to find
th_spr_found(k) = double(wrapToPi(round(S,3))); %recording vpasolve solution in each k = 1:N
end
I use above method. However it takes too much time. I considered maybe it is because I need to do this in fsolve or any other solution methods.
Do you know what is the fastest way to solve for th_spr, or more generally 1 highly nonlinear equation and 1 unknown?
Answers (1)
David Hill
on 10 Mar 2021
n1=1;%whatever you want for n1
n2=2;%whatever you want for n2
phi=-100000:.001:100000;%whatever you want for phi
n12=sqrt((2*n1^2*phi*(n2^2-n1^2)/(n2^2+2*n1^2)+n1^2)./(1-phi*(n2^2-n1^2)/(n2^2+2*n1^2)));
2 Comments
TUNAHAN YILMAZ
on 10 Mar 2021
David, my problem is highly nonlinear. I try to solve it numerically. What is your advice?
%% Defining some constants
th_spr_free = pi/2;
%%
th_spr = sym('th_spr',100); %Define th_spr symbolically (1x100)
assume(th_spr>pi/6 & th_spr<5*pi/6);
N = length(th_spr) % N = 100
th_spr_found = zeros(1,N);
T_spr_symbolic = -K *(th_spr-th_spr_free); % Nmm
for k=1:N
eqnLeft = T_spr_symbolic(k); %Linear equation dependent on th_spr
function_to_find_j4; %A function to find j4 in terms of th_spr
eqnRight = -(1000)*j4; %Highly nonlinear equation dependent on th_spr
S = vpasolve(eqnLeft == eqnRight, th_spr(k),[pi/6 5*pi/6]); % vpasolve to find
th_spr_found(k) = double(wrapToPi(round(S,3))); %recording vpasolve solution in each k = 1:N
end
I use vpasolve.
However it takes too much time. I considered maybe it is because I need to do this in fsolve or any other solution methods. But some of them are not numerical solution methods e.g. fzero.
Do you know what is the fastest way to solve for th_spr, or more generally 1 highly nonlinear equation and 1 unknown symbolically?
David Hill
on 11 Mar 2021
Seems like there are two questions here by two different authors. Recommend starting another question thread if you have a different question.
See Also
Categories
Find more on Systems of Nonlinear Equations 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!