solving equation using octave

流过昼夜 提交于 2019-12-08 03:56:18

问题


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.


回答1:


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.




回答2:


Type in these commands:

syms x

solve(x-3==2)

And you should get the following:

ans = (sym) 5

This was checked using an online version of Octave




回答3:


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



来源:https://stackoverflow.com/questions/25692490/solving-equation-using-octave

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!