解线性方程组

时间:2020-05-03 07:45:58

标签: julia

我有两个(数学)函数:

y = x
y = -2x + 3

此问题由y = 1x = 1解决。查看图片:

enter image description here

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

1 个答案:

答案 0 :(得分:6)

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

-x + y = 0
2x + y = 3

,您会看到它们的形式为线性方程组A*v=bA是一个矩阵:

julia> A = [-1 1; 2 1]
2×2 Array{Int64,2}:
 -1  1
  2  1

b是向量:

julia> b = [0, 3]
2-element Array{Int64,1}:
 0
 3

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

julia> A\b
2-element Array{Float64,1}:
 1.0
 1.0

如果您有一个更通用的非线性方程组,则应使用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

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

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

相关问题