3
votes

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.

3
Do you want to solve such equations symbolically or numerically? I.e., are you just interested in a numeric value for a solution (possibly with a small amount of floating-point error) rather than an exact analytic expression? - horchler
@horchler I'm interested in the numeric value for a solution. - Rick T
@RickT: x = num1-num2; For more complex equations there is linsolve, sqp and many other solver. - Andy
@Andy yes I know that's the formula but I need to solve equations I kept it simple in the question and including an example. "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?" Just listing off a bunch of commands like linsolve sqp and many others doesn't help much - Rick T
@RickT: But your example is much too easy to give you a good answer. I thought you'll see this when you read my answer. Do you want octave to solve arbitrary equations and you want to pass them as string? Is this the requirement you have? And what do you want to say with "these types of equations"? Univariate equations which consists of addition/substraction of constants? - Andy

3 Answers

3
votes

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.

3
votes

Type in these commands:

syms x
solve(x-3==2)

And you should get the following:

ans = (sym) 5
0
votes

See this discussion here: http://octave.1599824.n4.nabble.com/Newbie-question-on-solving-simple-equations-td1635574.html

Octave isn't really made to do those types of problems, however you might get away with using Fsolve. I would've put this in a comment as it isn't exactly a solution, but I don't have enough rep! sorry