Home > database >  MATLAB - exponential fucntion not solving
MATLAB - exponential fucntion not solving

Time:08-11

Trying to solve for p in the equation below (eqn), Matlab is taking forever to solve it so im unsure how to optimize or if a better solve function exists. It can plot it easy enough, just solving for any value of p is impossible it seems.

A_1 = 0.1044;
R_1 = 84.0038;
A_2 = 0.02667;
R_2 = 118.4852;
h_max_2 = 1.889;
q_s = 0.08;
a = 1/(A_2*R_2);
b = 1/(A_1*R_1);
c = 1/(A_1*A_2*R_1);

syms p
eqn = h_max_2 == q_s*c*1/(a*b)*(1 1/(a-b)*(b*exp(-a*p)-a*exp(-b*p)));
time_until = solve(eqn_2,p)

CodePudding user response:

How about moving h_max_2 over to the other side and find when the equation is zero? This can be solved with a simple call to fzero.

eqn = @(p) q_s*c*1/(a*b)*(1 1/(a-b)*(b*exp(-a*p)-a*exp(-b*p))) - h_max_2;
time_until = fzero(eqn, 0)
time_until = -2.7137

Alternatively, if you want a solution with a positive p, try using an initial guess of for instance p0 = 10.

time_until = fzero(eqn, 10)
time_until = 4.4923

eqn(time_until)
ans = 0

CodePudding user response:

Looking at the last line of your code, there is no eqn_2 variable, so I assume this is supposed to be eqn. Otherwise, if you do have a eqn_2 in your workspace, you could be trying to solve a very different function.

What version of MATLAB are you using? Your code executed for me in less than 0.1 seconds on R2021a. It did give the message Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve. and so your last line should probably be modified to be: time_until = vpasolve(eqn,p);. If your version of MATLAB doesn't automatically jump to vpasolve, that might explain what you are seeing.

For more complicated functions, you may get faster results by using simplify on your equation before solving, but in this case there was no time difference for me since it was already so fast.

The solution by @stewie-griffin where you rearrange to solve for when it is zero is likely a better and faster way to do this in general, but I wanted to attempt to address the issues with the approach you tried.

  • Related