whatchu mean I didn't save my comments :(
This commit is contained in:
@@ -2,20 +2,21 @@ A = [3, -0.1, -0.2; 0.1, 7, -0.3; 0.3, -0.2, 10]
|
|||||||
B = [7.85; -19.3; 71.4]
|
B = [7.85; -19.3; 71.4]
|
||||||
init = [0, 0, 0]
|
init = [0, 0, 0]
|
||||||
|
|
||||||
|
# gotta have an initial assumption (usually 0s)
|
||||||
function solution = gauss_seidel(A,b,initial)
|
function solution = gauss_seidel(A,b,initial)
|
||||||
for i = [1:length(A)]
|
for i = [1:length(A)]
|
||||||
sigma = 0;
|
sigma = 0;
|
||||||
for j = [1:length(A)]
|
for j = [1:length(A)]
|
||||||
if(j != i)
|
if(j != i)
|
||||||
|
# ze part where we have a32 - a31 etc
|
||||||
sigma = sigma + A(i,j) * initial(j)
|
sigma = sigma + A(i,j) * initial(j)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
endfor
|
endfor
|
||||||
|
# divide by elements coefff and the free term minus the summation above (rest of row)
|
||||||
initial(i) = (b(i) - sigma) / A(i,i)
|
initial(i) = (b(i) - sigma) / A(i,i)
|
||||||
endfor
|
endfor
|
||||||
solution = initial
|
solution = initial
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
balls = gauss_seidel(A,B,init)
|
balls = gauss_seidel(A,B,init)
|
||||||
balls = gauss_seidel(A,B,balls)
|
|
||||||
|
|||||||
@@ -3,20 +3,16 @@ clear clc
|
|||||||
pkg load symbolic;
|
pkg load symbolic;
|
||||||
|
|
||||||
|
|
||||||
f = @(x) 3*x.^2 - e.^x
|
f = @(x) 3*x.^2 - e.^x #example function we use for every single thing
|
||||||
a = 0
|
a = 0
|
||||||
b = 1
|
b = 1
|
||||||
syms x;
|
syms x;
|
||||||
ff = f(x)
|
|
||||||
function root = newton(f, x0)
|
function root = newton(f, x0)
|
||||||
if abs(f(x0)) < eps
|
if abs(f(x0)) < eps #Breaking condition
|
||||||
root = x0;
|
root = x0
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
syms x;
|
x0 = x0 -( f(x0) / deriv(f,x0) ); # dividing by the derivative
|
||||||
df = diff(f(x));
|
|
||||||
ff = function_handle (df);
|
|
||||||
x0 = x0 -( f(x0) / deriv(f,x0) )
|
|
||||||
newton(f, x0)
|
newton(f, x0)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
@@ -24,14 +20,14 @@ newton(f, 0.5)
|
|||||||
|
|
||||||
function root = secant(f, x0, x1)
|
function root = secant(f, x0, x1)
|
||||||
if abs(f(x0)) < eps
|
if abs(f(x0)) < eps
|
||||||
root = x0;
|
root = x0 # breaking condition
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
syms x;
|
syms x;
|
||||||
df = diff(f(x));
|
df = diff(f(x));
|
||||||
ff = function_handle (df);
|
ff = function_handle (df);
|
||||||
temp = x1;
|
temp = x1; # Temp variable for reassigning x0
|
||||||
x1 = x1 -( ( f(x1) * (x1 - x0) ) / ( f(x1) - f(x0) ) )
|
x1 = x1 -( ( f(x1) * (x1 - x0) ) / ( f(x1) - f(x0) ) ); #Formula that replaced y' in newton
|
||||||
x0 = temp;
|
x0 = temp;
|
||||||
secant(f, x0,x1)
|
secant(f, x0,x1)
|
||||||
endfunction
|
endfunction
|
||||||
|
|||||||
Reference in New Issue
Block a user