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

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 ?

1 Like

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

2 Likes

it dont work

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

2 Likes

#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”)

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

```
1 + 1
```
3 Likes

sounds like some uni homework…

5 Likes

Please check PSA: make it easier to help you
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?

2 Likes

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

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

X = A\B
2 Likes

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).

1 Like

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

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

yes :frowning: and my teacher told me code it with julia