Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ЧМ_2012 / ЧМ_Maxima_Excel / ЧМ_Excel_Maxima.pdf
Скачиваний:
198
Добавлен:
09.06.2015
Размер:
1.41 Mб
Скачать

f2:0.0;

eps:read("Please, enter epsilon: "); print("Epsilon = ", eps);

while (f1 - f2) >= eps do( f2 : f1,

i : i + 1.0,

f1 : f1 + ((3.0*i + 1)^2)/((4.0*i*i - 1.0)^2.0) );

print("Sum of series = ", f1);

Рис. 10. Результат задания 2.

Метод сопряженных градиентов

 

æ 3

4

0ö

æ

1ö

Решить систему Ax=B, где

ç

4

- 3

0

÷

ç

5

÷

A = ç

÷

B = ç

÷

 

ç

0

0

5

÷

ç

9

÷

 

è

ø

è

ø

Файл soprgrad.mac:

SoprGrad(A, n) :=block([y,x,Ax,Ap,r,r1,p,epsilon, k:0, atemp:0.0, btemp:0.0],

print("< Solution by conjugate gradients >"), print("Enter initial approximation for matrix:"), for i:0 thru n-1 do(

print("X[",i+1,"] = "), x[i]:read()

),

epsilon:read("Enter precision:"), if epsilon <= 0 then block(

print("Error! Precision must be more then 0"), break

),

Ax:MatrixByVector(A, x, n), for i:0 thru n-1 do(

r[i] : A[i, n] - Ax[i]

),

if Scal(r, r, n) # 0 then block(

for i:0 thru n-1 do p[i] : r[i], do(

Ap : MatrixByVector(A, p, n), atemp : Scal(r, p, n) / Scal(p, Ap, n), for i:0 thru n-1 do(

y[i] : x[i] + atemp * p[i], r1[i] : r[i] - atemp * Ap[i]

),

if NormaV(r1, n) <= epsilon then joe(

print("Amount of iterations: ", k), return (y)

),

btemp : Scal(r1, Ap, n) / Scal(p, Ap, n), for i:0 thru n-1 do(

p[i] : r1[i] - btemp * p[i], r[i] : r1[i],

x[i] : y[i]

),

k : k + 1

)

)

else block(

print("Amount of iterations: 1"), return (x)

)

);

MatrixByVector( H, V, n) := block([tmp,summa:0.0], for i:0 thru n-1 do(

for j:0 thru n-1 do

summa : summa + V[j] * H[i,j], tmp[i] : summa,

summa : 0.0

),

return (tmp)

);

NormaV(V, n):= block([summa:0.0],

for i:0 thru n-1 do summa : summa + V[i]*V[i], summa : sqrt(summa),

return (summa)

);

Scal(v1, v2, n):= block([summa:0.0], for i : 0 thru n-1 do

summa : summa + v1[i] * v2[i], return (summa)

);

n : 3;

array(A, n-1, n); for i:0 thru n-1 do(

for j:0 thru n do(

print("Enter A[", i, ",", j, "]: "), A[i,j]:read()

)

 

 

);

 

 

print("Coefficients of extended matrix:");

 

for i:0 thru n-1 do(

 

 

print(A[i, 0], "", A[i, 1], "

",A[i, 2],"

",A[i, 3])

);

temp : SoprGrad(A, n); for i:0 thru n-1 do

print("X[", i + 1, "] = ", temp[i]);

Рис. 11. Результат задания метода сопряженных градиентов.

Решение нелинейных уравнений

Требуется решить методом простой итерации нелинейное уравнение x2 - 11× x + 30 = 0 , интервал поиска корня [3, 5.5]и шаг h = 0.3 .

Требуется решить уравнение f (x) = 0 с точностью ε: f (x) = x3 + 2x2 + 3x + 5,

ε = 0.00001.

Файл nonlinear.mac:

coefficients(coefs, n) := block( [prived_coefs],

if coefs[1] = 0 then break("incorrect equation!!!"), for i : 0 thru n-1 do prived_coefs[i]:-coefs[i]/coefs[1], prived_coefs[1] : 0.0,

return (prived_coefs)

);

f(x, coefs, n):= block( [result:0.0],

for i:0 thru n-1 do result : result + (x^i)*coefs[i], return (result)

);

StepsMeth(st, coefs, n, a, b) := block( [x:0.0, x_prev:0.0, b_new:b], for i:a thru b-st step st do(

x_prev : x,

x : f(i, coefs, n),

if x*x_prev < 0 then joe( b_new : i, return()

),

 

if

x = 0 then joe(

b_new : i+st/2, return ()

)

),

return (b_new)

);

dir( x, coefs, n) := block( [result:0.0],

for i:1 thru n-1 do result : result + x^(i - 1)*i*coefs[i], return (result)

);

dirSecond(x, coefs, n) := block( [result:0.0],

for i:2 thru n-1 do result : result + x^(i - 2)*(i)*(i-1)*coefs[i], return (result)

);

halfDivision(coefs, n, st, at, bt, e) := block( [b:at, x:at, a:0.0],

do(

b : StepsMeth(st, coefs, n , b, bt), a : b - st,

if bt <= b then return(), x : a,

while (abs(f(x, coefs, n)) > e) do( x : (b+a)/2,

if f(x,coefs,n)*f(a,coefs,n) < 0 then b : x else a : x,

if f(x,coefs,n) = 0 then joe( print("Root: ", x), return()

)

),

print("Root: ", x)

)

);

Xord(coefs, n, st, at, bt, e) := block( [b:at, x:at, a:0.0],

do(

b : StepsMeth(st, coefs, n, b, bt), a : b - st,

if bt <= b then return(), x : a,

while (abs(f(x, coefs, n)) > e) do

x : -(f(x,coefs,n)*(b-x))/(f(b,coefs,n)-f(x,coefs,n)) + x, print("Root: ", x)

)

);

Casatel(coefs, n, st, at, bt, e) := block( [b:at, x:at, a:0.0],

do(

b : StepsMeth(st, coefs, n , b, bt), a : b - st,

if bt <= b then return(),

if dir(a, coefs,n) > 0 then x : b else x : a,

while (abs(f(x,coefs,n)) > e) do

x : -f(x,coefs,n)/dir(x, coefs, n) + x, print("Root: ", x)

)

);

Iteration(coefs, n, st, at, bt, e) := block( [b:at, x:at, a:0.0, prived_coefs], do(

b : StepsMeth(st, coefs, n , b, bt), a : b - st,

if bt <= b then return(), x : a,

prived_coefs : coefficients(coefs, n), while(abs(f(x,coefs,n)) > e) do x : f(x, prived_coefs ,n), print("Root: ", x)

)

);

Graph(coefs, n, at, bt):= block([f:0.0],

for i:0 thru n-1 do f : f + (x^i)*coefs[i], plot2d(f,[x, at, bt])

);

print("<Solution of nonlinear equation>"); n : read("Enter power of polynomial: "); array(coefs, n-1);

for i:0 thru n-1 do(

print("Enter ",i,"-th coefficient: "), coefs[i]: read()

);

st : 0.1;

at : read("Enter a: "); bt : read("Enter b: ");

e : read("Enter presicion: "); print("Chose method:"); print("1 - halfDivision,"); print("2 - Casatelnih,"); print("3 - Hord,");

print("4 - Iteracii,");

print("5 - graphical method."); method : read();

if method = 1 then halfDivision(coefs, n, st, at, bt, e); if method = 2 then Casatel(coefs, n, st, at, bt, e);

if method = 3 then Xord(coefs, n, st, at, bt, e);

if method = 4 then Iteration(coefs, n, st, at, bt, e); if method = 5 then Graph(coefs, n, at, bt);

Рис. 12. Метод деления отрезка пополам.

Рис. 13. Графический.

Рис. 14. Метод Хорд.

Рис. 15. Метод касательных (Ньютона).