Inverse of algebraic expression in Matlab
1 view (last 30 days)
Show older comments
I need to get inverse expression of the following algebraic expression in matlab.
clc;
clear;
syms r
E(r)= 1-1/2*((1+r)*log2(1+r)+(1-r)*log2(1-r));
g=finverse(E)
Using this command no output is there. I need to see the expression of r in terms of E. Is there any way to get that. Pl somebody help me.
output is
g(r) =
Empty sym: 0-by-1
2 Comments
Thiago Henrique Gomes Lobato
on 16 Feb 2020
The problem in your case is that matlab cannot find a close form to your inverse function, so it gives as an output a warning and the empty result, otherwise your code would give the correct result (you could interpret the "r" from g as E and g as "r"). For what reason do you want the inverse expression? If you just want the numeric values I would calculate E in the wished interval and then maybe fit a polynom for the inverse mapping.
Accepted Answer
Star Strider
on 17 Feb 2020
Possibly:
rfcn = @(E) fsolve(@(r) 1-1/2*((1+r).*log2(1+r)+(1-r).*log2(1-r)) - E, 10); % Anonymous Function: r(E)
N = 200;
Ev = linspace(0, 1, N); % Sample E’ Vector
for k = 1:numel(Ev)
r(k) = rfcn(Ev(k));
end
figure
plot(Ev, real(r), Ev, imag(r))
grid
gama = linspace(-1, 1, N);
[Gama,Rm] = ndgrid(gama,r);
G = @(r,gama) atan(r.*tan(N*gama)); %% r is [0,1];
g = @(r,gama) atan(r.*tan(gama)); %% gama is also [-1,1];
Delta=G(Rm,Gama)-N*g(Rm,Gama);
figure
meshc(gama, Ev, real(Delta))
grid on
xlabel('\gamma')
ylabel('E')
zlabel('\Delta')
view(125,25)
and:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/272224/image.png)
0 Comments
More Answers (1)
John D'Errico
on 16 Feb 2020
Edited: John D'Errico
on 16 Feb 2020
As is often the case, not all simple problems have an algebraic solution. In fact, as you have found, it is trivial to write one where that fails.
However, it is also easy to solve numerically, as long as you have some value that you wish to solve for. If you plot it, you will see the relation is symmetric around the y axis.
fplot(E,[-2,2])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/272008/image.jpeg)
As well, it has singularities at -1 and 1, and does not exceed 1, nor does it have real values for r outside of the open interval (-1,1). That should be clear just by inspection.
pretty(E)
log(1 - r) (r - 1) log(r + 1) (r + 1)
------------------ - ------------------ + 1
2 log(2) 2 log(2)
Since you are using a symbolic form, just use vpasolve.
vpasolve(E(r) == 0.5,r,.5)
ans =
0.77994427112328089747637659133002
See that I chose a start point greater than zero, so vpasolve found a positive solution.
I could also have used fzero, of course.
5 Comments
John D'Errico
on 17 Feb 2020
I'm sorry, but I will not keep chasing a moving target. This question is going far afield in the comments, morphing from one problem to another. I solved your first two questions, but I cannot play the role of long term MATLAB consultant to solve your every problem. If I did, then you will just return with another modified form with an extra variable ot two, but where you are not thinking for yourself.
I'll just suggest that if you think of it along the lines of how I showed you how to solve the first two problems, it will be clear.
See Also
Categories
Find more on Creating and Concatenating Matrices 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!