Return symbolic array elements that meet a condition

38 Views Asked by At

I have a function to return roots where a quadratic crosses 0:

function r=valid_quad_roots(q2,q1,q0)
    r=sort(roots([q2 q1 q0])).'; % first find all roots
    r=r(~imag(r)); % get only real roots

    % remove any roots that are tangents. Only actual crossing points
    slope=2*q2*r+q1;
    r=r(slope~=0);
end

q2, q1 and q0 depend on some parameter, say theta, related to the bigger problem I am solving. Across an array of theta values from say -pi to pi, I compute corresponding values of q2, q1 and q0, and call this function, and it returns a 0, 1 or 2-element array of valid roots. (Just FYI, the bigger picture is that I am integrating some downstream value across theta, and this function is called in the middle during that integral.)

This has worked fine with numeric theta, e.g.:

theta=-1; % theta = -1/0/1 give 2/1/0 valid roots respectively

q2=2*theta;
q1=theta+1;
q0=1;

r=valid_quad_roots(q2,q1,q0)

Now I need to extend this function to symbolic theta (as part of doing a vpaintegral across theta). Try this:

syms theta

q2=2*theta;
q1=theta+1;
q0=1;

r=valid_quad_roots(q2,q1,q0)

Now that r is a sym, it no longer likes the conditional indexing r(~imag(r)) or r(slope~=0).

How can I make this function similarly return a sym array of only those valid roots that meet my conditions?

0

There are 0 best solutions below