solve function for log?
Show older comments
I am trying to solve the following equation for y, where x and k are known
log(y)+log(((k-1-x(i))/(k-1-y*x(i)))^k)== log(4)
when I use solve function, it returns a warning of spurious solution, and subscripted assignment mismatch error.
the program works if I get rid of the k in the power
i.e
log(y)+log(((k-1-x(i))/(k-1-y*x(i))))== log(4)
I tried using vpasolve function too, but it did not help.
Any help will be appreciated
Here is the code:
clc; clear all;
syms y;
k=50;
x = 0.01:1:10;
[~,n] = size(x);
for i= 1:n;
eqn=log(y)+log(((k-1-x(i))/(k-1-y*x(i)))^k)== log(4);
z(i)=10*log10(solve(eqn,y));
end
figure(1)
set(gca, 'XScale', 'log')
semilogx(x,z)
Answers (2)
Star Strider
on 29 Mar 2018
You need to calculate a numeric solution to your equation. One way to understand what it is doing is to find and plot the contour of the roots (where the contour intersects 0 as I have written your function). A benefit is that the contour function returns the (x,y) coordinates of the roots as well.
k=50;
x = 0.01:1:10;
[~,n] = size(x);
y = linspace(0, 50, 150);
eqn = @(y,x) log(y)+log(((k-1-x)./(k-1-y.*x)).^k) - log(4);
[X,Y] = meshgrid(x,y);
Z = eqn(X,Y);
figure(1)
contour(X, Y, Z, [0 0])
grid on
set(gca, 'YLim', [-5 50])
xlabel('X')
ylabel('Y')
See the documentation on the contour (link) function for details. You can get the (x,y) coordinates of that contour by getting the first output from contour, for example:
C = contour(X, Y, Z, [0 0]);
Xv = C(1,2:end);
Yv = C(2,2:end);
figure(2)
semilogx(Xv, 10*log10(Yv))
grid
You can then convert the ‘y’-cordinates to dB, and plot them as s function of the ‘x’-coordinates. See the documentation on the ContourMatrix (link) property for details and a description of how the data are stored, and how to access them. I already did that here.
I defer to you to interpret the results, since I have no idea what you are doing.
Experiment to get the results you want.
Walter Roberson
on 29 Mar 2018
For positive integer k, there are k roots of the equation, most of them imaginary. In the range of x you are interested in, there are two real roots, both of which are positive.
x = 0.01:1:10;
syms X y;
k=50;
eqn = log(y)+log(((k-1-X)/(k-1-y*X))^k) == log(sym(4));
sol = solve(eqn, y);
z = subs(sol(1:2), X, x);
subplot(1,2,1)
semilogy(x, z(1,:))
title('lower branch')
subplot(1,2,2)
semilogy(x, z(2,:))
title('upper branch')
Categories
Find more on Calculus 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!