Search code examples
matlaboctavegnu

solving equation using octave


I have a simple equation I'm trying to solve

num1=-2
num2=-3

x+num2=num1
x+-3=-2
x=1

How can I do this in octave. In matlab I can do y = solve('x-3 = -2') but this doesn't work in octave 3.8.1 which is the version I'm using. How can I get octave to solve these types of equations?

I'm interested in the numeric value for a solution.


Solution

  • I'm assuming that the equation in your question is an example. If you're interested in a numeric solution, there is often no need to use symbolic math. In Octave (or Matlab), you can can use fzero to find a real root/zero of a nonlinear equation in terms of a single-variable free variable. For your simple linear example, using an anonymous function to represent your equation:

    num1 = -2;
    num2 = -3;
    f = @(x)x+num2-num1;
    x0 = 0; % Initial guess for x
    x = fzero(f,x0)
    

    If an equation has multiple roots/zeros you'll need to try different initial guesses in the vicinity of each zero to find the exact value.

    Octave also has a version of Matlab's fsolve to solve systems of nonlinear equations in multiple variables. If your equations are linear (e.g., A*x = b), you should look at linsolve.