finding the points which results in zero in simple formula

2 views (last 30 days)
Hello,i have a simple formula shown in the code bellow.
i want to find the points where they are exactly zero.
if we look at the values of "y",we have points where its a little above zero and a little bellow zero.
but there is no single point where i get exactly zero no matter how much sampling points i made in the imput.
Is there something that could be done in order to find the exact values which gives zeros?
Thanks.
[phi,theta] = meshgrid(0:0.001:2*pi,0:0.001:pi/4);
u = sin(theta).*cos(phi);
v = sin(theta).*sin(phi);
y=u+v-0.125;
k = find(~y);

Accepted Answer

John D'Errico
John D'Errico on 17 Dec 2021
Edited: John D'Errico on 17 Dec 2021
You have the phi and theta. Now you want to know, for which values of phi and theta, you will have y==0, exactly?
DON'T USE A LOOP!!!!!! Use mathematics instead.
syms phi theta
u = sin(theta).*cos(phi);
v = sin(theta).*sin(phi);
y=u+v-0.125;
Where is y == 0, exactly? The simplest way to see the locus of all solutions is to look at a plot.
fimplicit(y == 0)
grid on
xlabel phi
ylabel theta
So there are curved paths in the plane for phi and theta there this is true. They almost look like squares, but not quite. Can we show a formua for all solutions? Well, yes.
thetasol = solve(y == 0,theta,'returnconditions',true)
thetasol = struct with fields:
theta: [2×1 sym] parameters: k conditions: [2×1 sym]
thetasol.theta
ans = 
thetasol.conditions
ans = 
So for any value of phi, as long as cos(phi) + sin(phi) is not exactly zero, then there are infinitely many solutions, offset by integer multiples of 2*pi. The primary solutions are of the simple form shown, but with k=0.
Thus pick ANY value of phi, and that returns theta such that y == 0. There is absolutely no reason to use a loop. For example...
syms k
thetafun = matlabFunction(subs(thetasol.theta,k,0))
thetafun = function_handle with value:
@(phi)[asin(1.0./(cos(phi).*8.0+sin(phi).*8.0));pi-asin(1.0./(cos(phi).*8.0+sin(phi).*8.0))]
Now you can find the EXACT value for theta for ANY value of phi. For example, when phi is 1, we have two primary solutions, given as:
format long g
thetafun(1)
ans = 2×1
0.0905873085343786 3.05100534505541
Pick any value of phi, and thetafun returns the two solutions where y == 0. (As long as cos(phi) + sin(phi) is not zero, as then you would have a divide by zero.)
thetafun(0.5)
ans = 2×1
0.0922451756366426 3.04934747795315
NO loop required. Just some basic mathematics.

More Answers (1)

Walter Roberson
Walter Roberson on 17 Dec 2021
syms theta phi real
assume(theta >= 0 & theta <= sym(pi)/4)
assume(phi >= 0 & phi <= sym(pi)*2)
u = sin(theta).*cos(phi);
v = sin(theta).*sin(phi);
y = u+v-0.125;
ysol = solve(y, theta, 'returnconditions', true)
ysol = struct with fields:
theta: asin(1/(8*(cos(phi) + sin(phi)))) parameters: [1×0 sym] conditions: asin(1/(8*(cos(phi) + sin(phi)))) <= pi/4 & cos(phi) + sin(phi) ~= 0 & 0 <= asin(1/(8*(cos(phi) + sin(phi))))
ysol.theta
ans = 
fplot(ysol.theta, [0 2*pi])
Warning: Error updating FunctionLine.

Expected input number 2, edges, to be real.
LB = solve(cos(phi)+sin(phi) == -1/8, 'returnconditions', true)
LB = struct with fields:
phi: [2×1 sym] parameters: [1×0 sym] conditions: [2×1 sym]
vpa(LB.phi)
ans = 
UB = solve(cos(phi)+sin(phi) == 1/8, 'returnconditions', true)
UB = struct with fields:
phi: [2×1 sym] parameters: [1×0 sym] conditions: [2×1 sym]
vpa(UB.phi)
ans = 
PHI = linspace(0,2*pi, 500);
Y = double(subs(ysol.theta, phi, PHI));
plot(PHI, real(Y), 'b', PHI, imag(Y), 'r')
plot(PHI, imag(Y) == 0, 'g')
This tells us that for phi from 0 to 2*pi, there are gaps from about 2.267 to 2.444 and 5.409 to 5.586 where there is no real-valued solution for theta. Aside from that, however, for each phi in range there is exactly one theta value in theory
But in practice, in order to show up on your grid search, your quantized theta and your quantized phi would have to have exactly the right condition that phi = asin(1/(8*(cos(theta)+sin(theta)))) . At the moment I would not say that is completely impossible, but it is unlikely. The "nice" values for sin() and cos() purposes are 0 and at a small number of multiples pi like pi/6 -- but since pi is irrational and you quanitized, you can never exactly get a value such as pi/6

Community Treasure Hunt

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

Start Hunting!

Translated by