求解线性方程组

Geo*_*ery 2 julia

我有两个(数学)函数:

y = x
y = -2x + 3
Run Code Online (Sandbox Code Playgroud)

这由y = 1和解决x = 1。见图片:

在此处输入图片说明

我怎样才能让朱莉娅为我做这件事?

Bog*_*ski 7

这是一组线性方程,因此首先按以下方式重新排列它们:

-x + y = 0
2x + y = 3
Run Code Online (Sandbox Code Playgroud)

并且您看到它们以线性方程组的形式存在A*v=bA是一个矩阵:

julia> A = [-1 1; 2 1]
2×2 Array{Int64,2}:
 -1  1
  2  1
Run Code Online (Sandbox Code Playgroud)

b是一个矢量:

julia> b = [0, 3]
2-element Array{Int64,1}:
 0
 3
Run Code Online (Sandbox Code Playgroud)

现在v包含您的未知变量xy. 您现在可以使用左除法运算符求解系统\

julia> A\b
2-element Array{Float64,1}:
 1.0
 1.0
Run Code Online (Sandbox Code Playgroud)

如果你有一个更通用的非线性方程系统,你应该使用 NLsolve.jl 包:

julia> using NLsolve

julia> function f!(F, v)
           x = v[1]
           y = v[2]
           F[1] = -x + y
           F[2] = 2*x + y - 3
       end
f! (generic function with 1 method)

julia> res = nlsolve(f!, [0.0; 0.0])
Results of Nonlinear Solver Algorithm
 * Algorithm: Trust-region with dogleg and autoscaling
 * Starting Point: [0.0, 0.0]
 * Zero: [1.0000000000003109, 0.9999999999999647]
 * Inf-norm of residuals: 0.000000
 * Iterations: 2
 * Convergence: true
   * |x - x'| < 0.0e+00: false
   * |f(x)| < 1.0e-08: true
 * Function Calls (f): 3
 * Jacobian Calls (df/dx): 3

julia> res.zero
2-element Array{Float64,1}:
 1.0000000000003109
 0.9999999999999647
Run Code Online (Sandbox Code Playgroud)

(请注意,在f!我们定义了两个输出F[1]并且F[2]等于零 - 您必须以这种方式重新排列您的方程)。

有关如何使用 NLsolve.jl 的更多详细信息,请参阅https://github.com/JuliaNLSolvers/NLsolve.jl