Examen Lenguaje de Programacion - CFD - Jesus Cruz Contreras

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

MÉTODO DE JACOBI

function [x, iterations] = jacobi_method(A, b, x0, tolerance, max_iterations)

% A: Coefficient matrix

% b: Right-hand side vector

% x0: Initial guess

% tolerance: Convergence tolerance

% max_iterations: Maximum number of iterations

D = diag(diag(A));

R = A - D;

x = x0;

iterations = 0;

for k = 1:max_iterations

x_old = x;

x = D \ (b - R * x_old);
% Check for convergence

if norm(x - x_old, inf) < tolerance

iterations = k;

return;

end

end

error('Jacobi method did not converge within the maximum number of iterations');

end

% Coefficient matrix

A = [1, 1, 1, 1;

2, 3, 1, 2;

1, 2, 3, 1;

3, 1, 2, 4];

% Right-hand side vector

b = [10; 19; 20; 37];

% Initial guess

x0 = [0; 0; 0; 0];

% Tolerance

tolerance = 1e-5;

% Maximum number of iterations

max_iterations = 1000;

[x, iterations] = jacobi_method(A, b, x0, tolerance, max_iterations);

disp('Solution:');

disp(x);

disp('Iterations:');

disp(iterations);

MÉTODO DE GAUSS – SIEDEL

function [x, iterations] = gauss_seidel_method(A, b, x0, tolerance, max_iterations)

% A: Coefficient matrix

% b: Right-hand side vector


% x0: Initial guess

% tolerance: Convergence tolerance

% max_iterations: Maximum number of iterations

n = length(b);

x = x0;

iterations = 0;

for k = 1:max_iterations

x_old = x;

for i = 1:n

sum1 = 0;

for j = 1:i-1

sum1 = sum1 + A(i,j) * x(j);

end

for j = i+1:n

sum1 = sum1 + A(i,j) * x_old(j);

end

x(i) = (b(i) - sum1) / A(i,i);

end

% Check for convergence

if norm(x - x_old, inf) < tolerance

iterations = k;

return;

end

end

error('Gauss-Seidel method did not converge within the maximum number of iterations');

end

% Coefficient matrix

A = [1, 1, 1, 1;

2, 3, 1, 2;

1, 2, 3, 1;

3, 1, 2, 4];
% Right-hand side vector

b = [10; 19; 20; 37];

% Initial guess

x0 = [0; 0; 0; 0];

% Tolerance

tolerance = 1e-5;

% Maximum number of iterations

max_iterations = 1000;

[x, iterations] = gauss_seidel_method(A, b, x0, tolerance, max_iterations);

disp('Solution:');

disp(x);

disp('Iterations:');

disp(iterations);

end

MÉTODO SOR

function [x, iterations] = sor_method(A, b, x0, tolerance, max_iterations, omega)

% A: Coefficient matrix

% b: Right-hand side vector

% x0: Initial guess

% tolerance: Convergence tolerance

% max_iterations: Maximum number of iterations

% omega: Relaxation factor

n = length(b);

x = x0;

iterations = 0;

for k = 1:max_iterations

x_old = x;

for i = 1:n

sum1 = 0;
for j = 1:i-1

sum1 = sum1 + A(i,j) * x(j);

end

for j = i+1:n

sum1 = sum1 + A(i,j) * x_old(j);

end

x(i) = (1 - omega) * x_old(i) + (omega / A(i,i)) * (b(i) - sum1);

end

% Check for convergence

if norm(x - x_old, inf) < tolerance

iterations = k;

return;

end

end

error('SOR method did not converge within the maximum number of iterations');

end

% Coefficient matrix

A = [1, 1, 1, 1;

2, 3, 1, 2;

1, 2, 3, 1;

3, 1, 2, 4];

% Right-hand side vector

b = [10; 19; 20; 37];

% Initial guess

x0 = [0; 0; 0; 0];

% Tolerance

tolerance = 1e-5;

% Maximum number of iterations

max_iterations = 1000;

% Relaxation factors

omega_values = [0.8, 1.5];


for omega = omega_values

[x, iterations] = sor_method(A, b, x0, tolerance, max_iterations, omega);

disp(['Solution with omega = ', num2str(omega), ':']);

disp(x);

disp(['Iterations: ', num2str(iterations)]);

end
Triangle.h
#ifndef TRIANGLE_H

#define TRIANGLE_H

class Triangle {

private:

double x1, y1, x2, y2, x3, y3;

public:

// Constructor

Triangle(double x1, double y1, double x2, double y2, double x3, double y3);

// Método para calcular el área del triángulo

double area() const;

// Método para calcular el centroide del triángulo

void centroid(double &cx, double &cy) const;

};

#endif // TRIANGLE_H

Triangle.cpp
#include "Triangle.h"

#include <cmath>

// Constructor

Triangle::Triangle(double x1, double y1, double x2, double y2, double x3, double y3)

: x1(x1), y1(y1), x2(x2), y2(y2), x3(x3), y3(y3) {}

// Método para calcular el área del triángulo usando la fórmula de Herón

double Triangle::area() const {

return std::abs((x1*(y2 - y3) + x2*(y3 - y1) + x3*(y1 - y2)) / 2.0);

// Método para calcular el centroide del triángulo

void Triangle::centroid(double &cx, double &cy) const {

cx = (x1 + x2 + x3) / 3.0;

cy = (y1 + y2 + y3) / 3.0;

You might also like