Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

You are given a matrix A, vector b and scalar α. Calculate x such that it minimizes the residual for Ax≅b and x2 such that it minimizes the regularization condition. Also calculate the residual and

You are given a matrix A, vector b and scalar α. Calculate x such that it minimizes the residual for Axb and x2 such that it minimizes the regularization condition. Also calculate the residual and norm for both.

code below

import numpy as np

import scipy.sparse

INPUT:

  • A: numpy array of shape (m, n)
  • b: numpy array of shape (m,)
  • alpha: Python float

OUTPUT:

  • x: numpy array of shape (n,) that minimizes the residual.
  • x2: numpy array of shape (n,) that minimizes the L2-regularization condition.
  • norm_x: Norm of x.
  • norm_x2: Norm of x2.
  • residual_x: Norm of the residual of x.
  • residual_x2: Norm of the residual of x2.

n = np.random.randint(100, 200)

A = scipy.sparse.diags([-1,2,-1],[-1,0,1],shape=(n, n//4)).A

b = np.ones(n)

alpha = 1.5

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question