Search code examples
matlabequationsymbolic-math

How to get only the positive solution to a quadratic equation using the MATLAB Symbolic Toolbox?


I want to solve this equation in Matlab.

e=2;
while (e<30)
    syms v;
    solve('(v-e) = -(0.5*0.2*1.276*v^2*0.3)');
    e=e+1;
end

When I write for example "solve('(v-10) = -(0.5*0.2*1.276*v^2*0.3)');" it works. But I need variable "e" in this equation. In some cases, this equation has 2 solutions (negative and positive), but I need only positive solutions. What is the correct syntax? Thank you.


Solution

  • To add e to your equation you can concact it as a number to your equation: ['equation part one', num2str(e), 'end of your equation'].

    To only have the positive solution, you can add a condition to your equation ( v>=0 ).

    Here is an example of a complete solution to your problem:

    ans = zeros(1,size(2:29,2));
    i = 0;
    syms v;
    for e = 2:29
        i = i+1;
        ans(i) = solve(['(v-',num2str(e),') = -(0.5*0.2*1.276*v^2*0.3) and v>=0']);
    end