简体   繁体   English

使用octave解方程

[英]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 .在 matlab 中,我可以做y = solve('x-3 = -2')但这在我正在使用的八度音阶 3.8.1 中不起作用。 How can I get octave to solve these types of equations?我怎样才能得到八度音程来解决这些类型的方程?

I'm interested in the numeric value for a 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.在 Octave(或 Matlab)中,您可以使用fzero来根据单变量自由变量找到非线性方程的实根/零。 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. Octave 还有一个版本的 Matlab 的fsolve来求解多变量非线性方程组。 If your equations are linear (eg, A*x = b ), you should look at linsolve .如果您的方程是线性的(例如, A*x = b ),您应该查看linsolve

Type in these commands:输入这些命令:

syms x
solve(x-3==2)

And you should get the following:你应该得到以下信息:

ans = (sym) 5

See this discussion here: http://octave.1599824.n4.nabble.com/Newbie-question-on-solving-simple-equations-td1635574.html请参阅此处的讨论: 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. Octave 并不是真正用来解决这些类型的问题,但是您可能会使用 Fsolve。 I would've put this in a comment as it isn't exactly a solution, but I don't have enough rep!我会把它放在评论中,因为它不完全是一个解决方案,但我没有足够的代表! sorry对不起

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM