# How to solve Ax=b, with A,x,b are matrix

**URL:** https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663
**Category:** Machine Learning
**Tags:** question
**Created:** [May 23, 2021, 2:26am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663 "2021-05-23T02:26:19Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Van\_Kh\_i\_Ph\_m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/van_kh_i_ph_m/32/25372_2.png) [@Van\_Kh\_i\_Ph\_m](https://discourse.julialang.org/u/Van_Kh_i_Ph_m)
#### Post date: [May 23, 2021, 2:26am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/1 "2021-05-23T02:26:19Z")

</div>

How to solve Ax=b, with A,x,b are matrix

And, Can anyone help me solve this function with Newton method with backtracking line search ?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [May 23, 2021, 2:38am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/2 "2021-05-23T02:38:53Z")

</div>

`b\A` should do what you want. It will also give a least squares solution if you have an over-constrained system.

---

<div class="post-metadata">

### Author: ![Van\_Kh\_i\_Ph\_m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/van_kh_i_ph_m/32/25372_2.png) [@Van\_Kh\_i\_Ph\_m](https://discourse.julialang.org/u/Van_Kh_i_Ph_m)
#### Post date: [May 23, 2021, 2:46am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/3 "2021-05-23T02:46:50Z")

</div>

it dont work

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/e/1e072be4215fb67199460aeed48120dc8fb1a810.png)

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [May 23, 2021, 3:17am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/4 "2021-05-23T03:17:35Z")

</div>

If you post a code that people can copy-paste you might get a better answer.

---

<div class="post-metadata">

### Author: ![Van\_Kh\_i\_Ph\_m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/van_kh_i_ph_m/32/25372_2.png) [@Van\_Kh\_i\_Ph\_m](https://discourse.julialang.org/u/Van_Kh_i_Ph_m)
#### Post date: [May 23, 2021, 4:02am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/5 "2021-05-23T04:02:33Z")

</div>

#gradient method with backtracking

using Printf

using ForwardDiff

using LinearAlgebra

using Plots

function grad\_method\_backtracking(fObj, gObj, hObj, x0; ϵ = 1e-6, μ = 1e-5, maxits = 1000)

```
x = copy(x0)

f = fObj(x)

∇f = gObj(x)

∇²f=hObj(x)

d=-∇f\∇²f

k = 0

xtrace = x'

while norm(∇f) > ϵ && k < maxits

    α = 1.0

    while ((f - fObj(x-α*∇f)) < μ*α*dot(∇f,∇f) )

        α /=2

    end

    x = x+d

    f = fObj(x)

    ∇f = gObj(x)

    ∇²f = hObj(x)

    d=-∇f\∇²f

    k += 1

    xtrace = vcat(xtrace,x')

end

@printf "it = %3d | |∇f| = %8.2e | f = %8.2e\n" k norm(∇f) f

show(x)

return x, xtrace

```

end

#Apply gradient method with backtracking to Rosenbrock function

f(x) = 100(x[2]-x[1]^2)^2+(1-x[1])^2

∇f(x) = ForwardDiff.gradient(f,x)

∇²f(x) = ForwardDiff.hessian(f,x)

x0 = [1.2 1.2]

x, xtrace = grad\_method\_backtracking(f, ∇f, ∇²f, x0, μ = 1e-4, maxits = 1000);

#Contour plot

# f(x1,x2) = 100(x2-x1^2)^2+(1-x1)^2

# x1 = 0:0.05:3

# x2 = 3:0.05:6

# contour(x1, x2, f, levels = 50)

# plot!(xtrace[:,1],xtrace[:,2],marker = 3,legend = false, title = “Path of gradient method”)

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [May 23, 2021, 4:25am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/6 "2021-05-23T04:25:53Z")

</div>

You can format it like code by using backticks like this:

````julia
```
1 + 1
```

````

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [May 23, 2021, 5:08am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/7 "2021-05-23T05:08:24Z")

</div>

sounds like some uni homework…

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [May 23, 2021, 8:28am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/8 "2021-05-23T08:28:16Z")

</div>

Please check [PSA: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)  
In particular, format code using triple backticks to ease readability.  
Further, the point that mentions how to write a minimal reproducible example. Your example is too complex for the problem you are asking help with.

I can only see that you did a\b in the fifth line below `function grad...`  
That line will run correctly if you do (`permutedims(yourgradient)\yourhessian`). This is because of the shape of the arrays you are trying to use.

But how did you end up writing all of that code before managing to run the first 5 lines inside your function? Did it work before and you did some change?

---

<div class="post-metadata">

### Author: ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)
#### Post date: [May 23, 2021, 10:41am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/9 "2021-05-23T10:41:59Z")

</div>

Solving the linear equation `AX=B` where all the terms are matrices is as straightforward as calling

```julia
n = 3
B = rand(n,n)
A = rand(n,n)

X = A\B

```

---

<div class="post-metadata">

### Author: ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)
#### Post date: [May 23, 2021, 10:47am UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/10 "2021-05-23T10:47:37Z")

</div>

> [@Van\_Kh\_i\_Ph\_m](#):
>
> Can anyone help me solve this function with Newton method with backtracking line search ?

I do not quite get it. Which _function_ (did you mean _equation_?) do you want to solve with Newton method? The linear one? Why would you use Newton method for it? The key idea behind the (root-finding) Newton method is just that that of a linearization – in every iteration a nonlinear function is approximated by a linear one and a linear equation of the kind `Ax=b` has to be solved).

---

<div class="post-metadata">

### Author: ![Van\_Kh\_i\_Ph\_m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/van_kh_i_ph_m/32/25372_2.png) [@Van\_Kh\_i\_Ph\_m](https://discourse.julialang.org/u/Van_Kh_i_Ph_m)
#### Post date: [May 23, 2021, 3:42pm UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/11 "2021-05-23T15:42:36Z")

</div>

i need solve Ax=b with A is matrix 2x2, x is matrix 2x1 and b is matrix 2x1 ☹

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 23, 2021, 4:08pm UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/12 "2021-05-23T16:08:37Z")

</div>

Sounds like that’s doable by hand - is this a homework problem?

---

<div class="post-metadata">

### Author: ![Van\_Kh\_i\_Ph\_m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/van_kh_i_ph_m/32/25372_2.png) [@Van\_Kh\_i\_Ph\_m](https://discourse.julialang.org/u/Van_Kh_i_Ph_m)
#### Post date: [May 23, 2021, 4:16pm UTC](https://discourse.julialang.org/t/how-to-solve-ax-b-with-a-x-b-are-matrix/61663/13 "2021-05-23T16:16:10Z")

</div>

yes ☹ and my teacher told me code it with julia
