# 牛頓拉夫森解非線性方程式(matlab)

Newton-Raphson牛頓拉夫森解非線性方程式(matlab)

y^3=x2^2-x1*x2+4x1

y^2=x^3-2x2^2

y=(x1^2/x2)+0.4

y=x1=x2=10 迭代幾次能達到誤差1%

Update:

y=x1=x2=10 為起始猜測值

Rating
• Elisha
Lv 6
7 years ago

其中你第2條方程式y^2=x^3-2x2^2 (是x1或x2)不曉得。為求解，我先設x1

我將你的方程式整理如下(設y為x3)

f1 = x3^3 - x2^2 + x1*x2 - 4*x1 = 0

f2 = x3^2 - x1^3 + 2*x2^2 = 0

f3 = x3*x2 - x1^2 - 0.4*x3 = 0

jacobian matrix

df/dx = [df1/dx1, df1/dx2, df1/dx3;

df2/dx1, df2/dx2, df2/dx3;

df3/dx1, df3/dx2, df3/dx3]

Hence

df/dx = [(x2 - 4), (1 - 2*x2), 3*x3^2;

2*x1^2, 4*x2, 2*x3;

-2*x1, x3, (x2 - 0.4)]

---------------------------------------------------------------------

程式碼

function [x Tolx] = newton(fun,x0)

k = 0; kmaxit = 5000; Tolx = 1;

while k < kmaxit & Tolx > 1/100

[f df] = feval(fun, x0);

x = x0 - df\f;

Tolx = norm(f,2);

x0 = x;

k = k + 1;

end

function [f df] = model(x)

f = zeros(length(x),1);

df = zeros(length(x));

f(1) = x(3)^3 - x(2)^2 + x(1)*x(2) - 4*x(1);

f(2) = x(3)^2 - x(1)^3 + 2*x(2)^2;

f(3) = x(3)*x(2) - x(1)^2 - 0.4*x(3);df(1,:) = [(x(2) - 4), (1 - 2*x(2)), 3*x(2)^2];

df(2,:) = [3*x(1)^2, 4*x(2), 2*x(3)];

df(3,:) = [-2*x(1), x(3), (x(2) - 0.4)];

clear all

clc% 1. Newton

x_init = [1; 1; 1];

[x Tolx] = newton(@model,x_init)

--------------------------------------------------------------------------------

結果

若你的初始值皆為10，則發散，因此我皆改為1

但結果

x = -0.000877796781624

0.003504068831354

0.000000018550377

Tolx = 0.006976900252176

------------------------------------------結果x皆非常接近於0，其實若誤差設為1e-10則

x = 1.0e-10 * -0.065259394915470

0.261037579657942

0.000000000000000

Tolx = 5.220751592981038e-11

------------------------------------------因此是否方程式有誤？

Source(s): me