Search code examples
matlabequationfractionsequation-solvingquadratic

Solve quadratic equations without individually specifying cofficients


I am trying to solve equations like:

3*(3x-12)/(x+3)-2*(2x+3)/(3x-1) = 5

This is the code that I use:

eqn1 = 3*(3*X-12)/(X+3)-2*(2*X+3)/(3*X-1) == 5;
sol = solve(eqn1, X);
xSol = sol.X

This is the error that I get:

Error using sym/subsref
Too many output arguments.

Solution

  • The first thing I'd suggest is performing a graphical solution:

    % Define the function:
    f = @(X)3*(3*X-12)./(X+3)-2*(2*X+3)./(3*X-1)-5;
    % Plot the function (solve graphically):
    x = -30:0.1:30;
    figure(); plot(x,f(x)); grid on; grid minor;
    

    Crude plot

    This function has vertical asymptotes and a horizontal asymptote, at x=-3, x=1/3 and y=8/3 (finding this is left as an exercise to the reader). Let's add them to the chart and zoom to the y-vicinity of 0:

    hold on; plot([-3, -3, NaN, 1/3, 1/3], 600*[-1, 1, NaN, -1, 1],'--r');
    plot([-30, 30], 8/3*[1 1], '--m'); ylim([-10 10]);
    

    Zoom in with asymptotes

    There appear to be two solution, one between the vertical asymptotes and another to the right of the right asymptote. We can define these regions for fzero:

    % Find zeros:
    z = [ fzero(f, [-3+eps(3) 1/3-eps(1/3)] ),... First solution
          fzero(f, [1/3+eps, 30])];             % Second solution
    

    (where 30 is some sufficiently large number) and we get:

    z =
    
        0.1902   21.6848