vpasolve gives me only 1 x-value of intersection

8 views (last 30 days)
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))
ans = 
0

Answers (2)

Star Strider
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
ixz = 1×6
893 1073 5000 7270 8542 8927
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
xz = 1×6
-1.6428 -1.5715 0.0000 0.9083 1.4169 1.5707
yz
yz = 1×6
14.8623 11.8185 1.0000 2.2818 7.4444 11.7888
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
xf = 1×6
-1.6428 -1.5708 -0.0000 0.9083 1.4169 1.5708
yf
yf = 1×6
14.8623 11.7918 1.0000 2.2818 7.4445 11.7918
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
Jordi van Selm
Jordi van Selm on 14 Dec 2022
Thanks again, I was kinda vage about my question. I would like to get the y-value of the intersection near 1.5*pi (which is also the asymptote of tan(x)+1) with the function e^x^2 and not with tan(x)+1 since they don't intersect on the asymptote. So lets say I chose pi*1.5-0.1 (left side of asymptote), what is my y-value when intersecting with e^x^2 ?
Star Strider
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)]
Evaluated = 
vpaEvaluated = vpa(Evaluated)
vpaEvaluated = 
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
ixz = 1×6
3219 3297 5000 5985 6536 6703
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
xz = 1×6
-1.6428 -1.5724 0.0000 0.9083 1.4169 1.5705
yz
yz = 1×6
14.8623 11.8514 1.0000 2.2818 7.4444 11.7825
% 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
xf = 1×6
-1.6428 -1.5708 0.0000 0.9083 1.4169 1.5708
yf
yf = 1×6
14.8623 11.7918 1.0000 2.2818 7.4445 11.7918
Lv2 = ismembertol(abs(xf), pi/2, 0.01)
Lv2 = 1×6 logical array
0 1 0 0 0 1
xf = xf(~Lv2)
xf = 1×4
-1.6428 0.0000 0.9083 1.4169
yf = yf(~Lv2)
yf = 1×4
14.8623 1.0000 2.2818 7.4445
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.
.

Sign in to comment.


Walter Roberson
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
Jordi van Selm
Jordi van Selm on 4 Dec 2022
I found this, but I'm having trouble writing code to get all the values:
syms x y
[sol2x,sol2y] = vpasolve(y-tan(x)-1 == 0,y-exp(x^2) == 0, [-2,2])
sol2x = 
sol2y = 
14.862250796440579193799274998889
Walter Roberson
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.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!