numeric solve issue for an equation involving a logarithm

I am trying to solve an equation involving a common logorithm within a loop. I first solve an equation to get r(i). then I take that r(i) value and plug it into an equation to solve for b(i).The second equation is:
b(i)=solve('r(i)=.5*Log10(b(i)) + .5*b(i)')
It is having a lot of trouble solving this. This is the error message:
Warning: Could not find an exact (case-sensitive) match for 'Log10'.
/Applications/MATLAB_R2009aSV.app/toolbox/matlab/elfun/log10.m is a
case-insensitive match and will be used instead.
You can improve the performance of your code by using exact
name matches and we therefore recommend that you update your
usage accordingly. Alternatively, you can disable this warning using
warning('off','MATLAB:dispatcher:InexactCaseMatch').
This warning will become an error in future releases.
> In testloopwithgraph at 6
??? Error using ==> mupadengine.mupadengine>mupadengine.feval at 162
Error: no indeterminate(s) [numeric::solve]
Error in ==> solve>mupadSolve at 232
list = feval(symengine,'mlfsolve',eqns,vars);
Error in ==> solve at 93
[R,symvars,order] = mupadSolve(eqns,vars);
Error in ==> testloopwithgraph at 7
b(i)=solve('r(i)=.5*Log10(b(i)) + .5*b(i)');
I can numericaly solve this equation in mathematica with just a warning about how the output might not cover all values, but it does give me values. In my matlab code it won't give me values at all. Is there a way around this?

4 Comments

b(i) is the new variable i want to solve for. here is the complete loop:
p=input('TauesNtm1 Value?');
n=input('Nt Value?');
t=input('Taues1 Value?');
for i=(2:1:((n/2)-2)/1);
r(i)=.5*log10((2*p)^((1 - 2*(i - 2))/(n - 4))*(t/2)^((2*(i - 2))/(n - 4)))+(i - 2)*(((n - 4)*p-2*p-.5*t)/(n - 4));
b(i)=solve('r(i)=.5*log10(b(i)) + .5*b(i)');
end
thanks for catching that. that was a typo

Sign in to comment.

Answers (4)

use
log10
not
Log10

6 Comments

thanks for the tip.I changed it, but i am still getting an error message of:
??? Error using ==> mupadengine.mupadengine>mupadengine.feval at 162 Error: no indeterminate(s) [numeric::solve]
Error in ==> solve>mupadSolve at 232 list = feval(symengine,'mlfsolve',eqns,vars);
Error in ==> solve at 93 [R,symvars,order] = mupadSolve(eqns,vars);
Error in ==> testloopwithgraph at 7 b(i)=solve('r(i)=.5*log10(b(i)) + .5*b(i)');
() is used for function calls in MuPAD. Use [] for indexing. Or better yet, use simple variables since you aren't importing any value from the workspace.
b(i) = solve('r=1/2*log10(b)+1/2*b','b');
Remember, when you use a string argument to solve(), values are not imported from the MATLAB workpace! If you want to import the value of r(i) then don't use a string argument to solve:
syms B
[...]
r(i) = [...]
b(i) = solve(r(i) = 1/2*log10(B)+1/2*B, B);
p=1;
n=1;
t=1;
n=10
for i=(2:1:((n/2)-2)/1);
r(i)=.5*log10((2*p)^((1 - 2*(i - 2))/(n - 4))*(t/2)^((2*(i - 2))/(n - 4)))+(i - 2)*(((n - 4)*p-2*p-.5*t)/(n - 4));
b(i)=solve(['-' num2str(r(i)) '+.5*log10(b) + .5*b'],'b');
end
could you explain this a bit more?
syms B
[...]
r(i) = [...]
b(i) = solve(r(i) = 1/2*log10(B)+1/2*B, B);
do you want to use symbolic toolbox. In your case, implicit solution could'nt be found, unless you use just
syms b
%with r known
syms b
sol=solve(-r(1)+0.5*log10(b)+0.5*b,b)
so this code would go within the loop and without noting r and b as r(i) and b(i)?

Sign in to comment.

If I understand your intent correctly, you're trying to solve the equation
r_i = (Log10(b) + b)/2
for b, given a (numeric?) value of r_i. Then you want to store that b value as b_i. (Repeat for i+1)
If so, then (1) you don't need to do this symbolically and (2) you are getting the error because solve can't figure out what the variable is.
I'd suggest using fzero:
for i = 2:n
% get r(i)
% solve for b(i) using b(i-1) as an initial guess
b(i) = fzero(@(b) (log10(b) + b)/2 - r(i),b(i-1));
end

4 Comments

is b(i) = fzero(@(b) (log10(b) + b)/2 - r(i),b(i-1));
really the correct way to type it in? I am getting all kinds of syntax errors popping up
Yes, assuming you want to use the previous value for b as your initial guess. What errors do you get? It's entirely possible that fzero is failing to find the root.
i don't want to use previous values of b. the first equation gives me a value for r(i). I then want to plug in r(i) into the next equation to solve for b. this gives me one value for r(i) at that iteration and one value for b(i) at that iteration. I then want it to return to the beginning of the loop and do it all over again with i+1, giving me two new values for r(i+1) and b(i+10
Yes, I understand that, but fzero is a numerical solver, so it requires an initial guess for the solution. When doing stuff like this in a loop, it's not uncommon to use the previous solution as the starting point for the next solution. If r(i) varies somewhat slowly and smoothly with i, that would make sense. But it looks like maybe that's not the case here.
So, see my new answer...

Sign in to comment.

Didn't see your comment with your code. This works:
imax = (n/2)-2;
r = zeros(imax,1);
syms b
for i = 2:imax;
r(i)=.5*log10((2*p)^((1 - 2*(i - 2))/(n - 4))*(t/2)^((2*(i - 2))/(n - 4)))+(i - 2)*(((n - 4)*p-2*p-.5*t)/(n - 4));
bsolve(i)=solve((log10(b) + b)/2 == r(i),b);
end
bnum = double(bsolve);

8 Comments

here is the revised code, per your instruction:
clear all;
p=input('TauesNtm1 Value?');
n=input('Nt Value?');
t=input('Taues1 Value?');
(5)imax = (n/2)-2;
(6)r = zeros(imax,1);
(7)syms b
(9)for i = 2:imax;
(10) r(i)=.5*log10((2*p)^((1 - 2*(i - 2))/(n - 4))*(t/2)^((2*(i - 2))/(n - 4)))+(i - 2)*(((n - 4)*p-2*p-.5*t)/(n - 4));
(11) bsolve(i)=solve((.5*log10(b) + b)/2 == r(i),b);
(12)
(14)bnum = double(bsolve);
here is the error message I am getting:
Warning: Size vector should be a row vector with integer elements. > In testloopwithgraph2 at 6 ??? Error using ==> char Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 160 vc = char(v);
Error in ==> solve at 84 [eqns,vars] = getEqns(varargin{:});
Error in ==> testloopwithgraph2 at 11 bsolve(i)=solve((.5*log10(b) + b)/2 == r(i),b);
Either update your MATLAB version to R2012a or later, or use
bsolve(i)=solve((.5*log10(b) + b)/2 - r(i),b);
after the adjustment to subtracting r from both sides as suggested, this is working for giving me the a values, but still gives no b (tau) values except to say conj(b)
Warning: Size vector should be a row vector with integer elements. > In testloopwithgraph2 at 6 Q values Columns 1 through 7
0 -0.0236 -0.0182 -0.0128 -0.0074 -0.0020 0.0035
Columns 8 through 14
0.0089 0.0143 0.0197 0.0252 0.0306 0.0360 0.0414
Columns 15 through 21
0.0468 0.0523 0.0577 0.0631 0.0685 0.0739 0.0794
Columns 22 through 23
0.0848 0.0902
Tau Values conj(b)
By solving the general case in advance, you can avoid having to do the solve() each time. The general solution is
b(i) = (1/2)*10000^r(i)*exp(-4*r(i)*ln(10))*lambertw(2*ln(10)*exp(4*r(i)*ln(10)))/ln(10)
However, lambertw() is defined only by the Symbolic Math toolbox, which gives a fair bit of overhead if you need to do this many times. For MATLAB code for a numeric version of the principle branch of lambertw(), see http://www.mathworks.com/matlabcentral/newsreader/view_thread/32527
that was a great step forward. Thanks. Unfortunately, I tried to invert the function as well in mathematica and it spit out the wrong answers for b, just like it has now done with the new code. It works if i manually substitute a[[1]] into the original r(1)=0.5*log10(b)+0.5*b and gives me the right answer, but not if i try to invert that function.
I'm not sure what you mean about inverting the function? Do you mean that given a particular b value, you want to find the r value ?
no. r(i)=0.5*log10(b)+0.5*b is the equation i am using. I know r(i) from the previous equation in the loop. I want to solve for b. When I try to invert the function and get b(i) in terms of r(i), it spits out the wrong values.
b(i) in terms of r(i) is the formula I show above, involving LambertW .
I tested in Maple and the results appear to be correct. However, in cases where r(i) is complex, there can be multiple solutions.

Sign in to comment.

Thanks so much to all of you. I believe with your help I have found what is wrong and will embark on tracking down and fixing the problem. Thanks again for all your help!

Tags

Asked:

on 21 Sep 2012

Community Treasure Hunt

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

Start Hunting!