File:Kaczmarz iteration in R^2.png

From testwiki
Jump to navigation Jump to search
Kaczmarz_iteration_in_R^2.png (476 × 459 pixels, file size: 39 KB, MIME type: image/png)

This file is from Wikimedia Commons and may be used by other projects. The description on its file description page there is shown below.

Summary

Description
English: Kaczmarz iteration in

Python

import numpy as np
import matplotlib.pyplot as plt

# Randomly generate an intersection point
intersection = np.random.uniform(-5, 5, 2)

# Randomly generate line angles through the intersection point
num_lines = 3
angles = np.random.uniform(0, np.pi, num_lines)
slopes = np.tan(angles)

# Define the lines: y = m(x - x0) + y0
def line(x, m, x0, y0):
    return m * (x - x0) + y0

# Plot the lines
x_vals = np.linspace(-10, 10, 400)
for m in slopes:
    plt.plot(x_vals, line(x_vals, m, intersection[0], intersection[1]), label=f'Line')

# Kaczmarz algorithm
def kaczmarz(A, b, x0, max_iter=20):
    x = x0
    history = [x0]
    for k in range(max_iter):
        i = k % A.shape[0]
        ai = A[i, :]
        bi = b[i]
        x = x + (bi - np.dot(ai, x)) / np.linalg.norm(ai)**2 * ai
        history.append(x)
    return np.array(history)

# Construct A and b from the lines
A = np.column_stack((-slopes, np.ones(num_lines)))
b = -slopes * intersection[0] + intersection[1]

# Initial guess
x0 = np.random.uniform(-5, 5, 2)

# Run Kaczmarz algorithm
history = kaczmarz(A, b, x0, max_iter=20)

# Plot the iterations
plt.scatter(history[:, 0], history[:, 1], color='red', zorder=5)
plt.plot(history[:, 0], history[:, 1], color='red', linestyle='--', label='Kaczmarz Iterations')

# Mark the intersection point
plt.scatter(intersection[0], intersection[1], color='green', marker='*', s=200, label='Solution', zorder=10)

# Set aspect ratio 1:1
plt.gca().set_aspect('equal', adjustable='box')

# Add labels, legend, and grid
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid()
plt.title('Kaczmarz Algorithm in $\mathbb{R}^2$')
plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.show()

Date
Source Own work
Author Cosmia Nebula

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

22 January 2025

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current05:22, 23 January 2025Thumbnail for version as of 05:22, 23 January 2025476 × 459 (39 KB)wikimediacommons>Cosmia NebulaUploaded while editing "Kaczmarz method" on en.wikipedia.org

The following page uses this file: