vpasolve gives me only 1 x-value of intersection
8 views (last 30 days)
Show older comments
I would like to get all 4 points of intersection on the domain -2 < x < 2, but my code is only giving me only 1 value
( exp(x^2) is e^(x^2) right? )
syms x
vpasolve(exp(x^2) == 1+tan(x))
0 Comments
Answers (2)
Star Strider
on 4 Dec 2022
An analytic approach is an option, however it will be necessary first to estimate the approximate zero-crossings in any event.
A numeric approach —
syms x
xv = [-2 2];
figure
fplot(exp(x^2), xv)
hold on
fplot(1+tan(x), xv)
hold off
grid
ylim([-1 16])
clearvars
xv = linspace(-2, 2, 1E+4);
fx = exp(xv.^2) - (1+tan(xv));
L = numel(xv);
ixz = find(diff(sign(fx))) % Indices (Into 'xv') Of Approximate Zero-Crossings
for k = 1:numel(ixz)
idxrng = max(ixz(k)-1,1) : min(ixz(k)+1,L);
xz(k) = interp1(fx(idxrng),xv(idxrng),0); % Use: 'interp1'
yz(k) = exp(xz(k)^2);
end
xz
yz
figure
plot(xv, exp(xv.^2))
hold on
plot(xv, 1+tan(xv))
plot(xz, yz, 'sr')
hold off
ylim([-1 16])
f = @(x) exp(x.^2) - (1+tan(x));
for k = 1:numel(ixz)
xf(k) = fzero(f,xv(ixz(k))); % Use: 'fzero'
yf(k) = exp(xf(k)^2);
end
xf
yf
figure
plot(xv, exp(xv.^2))
hold on
plot(xv, 1+tan(xv))
plot(xf, yf, 'sr')
hold off
ylim([-1 16])
This approach also detects the intersections (zero-crossings) at so those would have to be eliminated afterwards. I did not try this with vpasolve, so I do not know if it would also detect them. This also requires a relatively high resolution in ‘xv’ in order to detect all the zero-crossings.
.
8 Comments
Star Strider
on 14 Dec 2022
syms x
xv = [-1 1]*(pi*1.5-0.1);
f1(x) = exp(x^2);
f2(x) = 1+tan(x);
Evaluated = [f1(xv); f2(xv); f1(xv)-f2(xv)]
vpaEvaluated = vpa(Evaluated)
figure
fplot(exp(x^2), xv)
hold on
fplot(1+tan(x), xv)
hold off
grid
ylim([-1 16])
clearvars
xv = linspace(-(pi*1.5-0.1), (pi*1.5-0.1), 1E+4);
fx = exp(xv.^2) - (1+tan(xv));
L = numel(xv);
ixz = find(diff(sign(fx))) % Indices (Into 'xv') Of Approximate Zero-Crossings
for k = 1:numel(ixz)
idxrng = max(ixz(k)-1,1) : min(ixz(k)+1,L);
xz(k) = interp1(fx(idxrng),xv(idxrng),0); % Use: 'interp1'
yz(k) = exp(xz(k)^2);
end
xz
yz
% Lv1 = ismembertol(abs(xz), pi/2, 0.01)
% xz = xz(~Lv1)
% yz = yz(~Lv1)
figure
plot(xv, exp(xv.^2))
hold on
plot(xv, 1+tan(xv))
plot(xz, yz, 'sr')
hold off
ylim([-1 16])
f = @(x) exp(x.^2) - (1+tan(x));
for k = 1:numel(ixz)
xf(k) = fzero(f,xv(ixz(k))); % Use: 'fzero'
yf(k) = exp(xf(k)^2);
end
xf
yf
Lv2 = ismembertol(abs(xf), pi/2, 0.01)
xf = xf(~Lv2)
yf = yf(~Lv2)
figure
plot(xv, exp(xv.^2))
hold on
plot(xv, 1+tan(xv))
plot(xf, yf, 'sr')
hold off
ylim([-1 16])
Looking at ‘(pi*1.5-0.1)’ and ‘vpaEvaluated’, the functions are not equal (and actually differ by more than ) at those values, so we can say with confidence that there is no intersection at that point. It’s not even close.
.
Walter Roberson
on 4 Dec 2022
vpasolve() only ever returns multiple results in the case of where the expressions are all polynomials, and there is the same number of equations as there are variables.
In all cases in which there are more equations than variables, or fewer equations than variables, vpasolve() will error.
In all cases involving a non-linear function of any variable, vpasolve() will try to find a single numeric solution, even if there might be multiple solutions. This includes cases that could be transformed into polynomials by a change of variable: vpasolve() will not even try to find multiple solutions if there is any nonlinear function of one of the variables.
You will need to provide multiple initial guesses to vpasolve()
2 Comments
Walter Roberson
on 4 Dec 2022
As far as vpasolve() is concerned, you gave it the same initial conditions as if you had specified
[-2, 2;
-inf, inf]
So you restricted x to be between -2 and +2, and you did not restrict y.
If you wanted to give starting values, then give a column vector such as
[1; 10]
You cannot give starting values and bounds ranges -- only starting values or bounds ranges.
See Also
Categories
Find more on Function Creation 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!