MATLAB與數學實驗 MATLAB 求解方程

 

 

 

 

一.常用函數

MATLAB裏solve、fzero、fsolve、root等函數用於求解方程(組)

1.solve函數

主要用於求解代數方程和非線性方程

[x1,……]=solve(‘F’,……,‘var’,……)

F表示方程,var表示求解變量。 x1表示求解結果。

求解單個方程

>> syms x a b c
>> ff=a*x^2+b*x+c;
>> solve(ff)
 
ans =
 
 -(b + (b^2 - 4*a*c)^(1/2))/(2*a)
 -(b - (b^2 - 4*a*c)^(1/2))/(2*a)

求解方程組

>> syms x  y a b
>> f1=x^2+2*y-a;
>> f2=3*x-4*y-b;
>> [x,y]=solve(f1,f2,x,y)
 
x =
 
 - (2*((9*a)/4 + (9*b)/8 + 81/64)^(1/2))/3 - 3/4
   (2*((9*a)/4 + (9*b)/8 + 81/64)^(1/2))/3 - 3/4
 
 
y =
 
 - b/4 - ((9*a)/4 + (9*b)/8 + 81/64)^(1/2)/2 - 9/16
   ((9*a)/4 + (9*b)/8 + 81/64)^(1/2)/2 - b/4 - 9/16

2.fzero函數

用於求非線性方程的最優解。兩種格式

function f=exam2_10(x)
f=x+2*sin(x)*exp(x)-1;

>> fzero(@exam2_10,[0,1])   第一個參數爲求解方程,第二個是求解區間

ans =

    0.2774

>> fzero(@exam2_10,0.8)    同上,第二個參數爲迭代初值

ans =

    0.2774

>> test=@(x)[x+2*sin(x)*exp(x)-1];
>> fzero(test,0.8);
>> fzero(test,0.8)

ans =

    0.2774

3 fsolve

用於求解非線性方程組的最優解。

function f=exam2_11(x)
f(1)=x(1)-sin(x(1))-5*x(2);
f(2)=2*x(1)+x(2)-cos(x(2));

>> fsolve(@exam2_11,[0,0])

Equation solved.

fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.

<stopping criteria details>


ans =

    0.4980    0.0041

4.roots函數

用於在複數範圍內求多項式方程的所有根。


>> syms x
>> a=[1,-6,0,5,-3,8];
>> roots(a)

ans =

   5.8626 + 0.0000i
  -1.2901 + 0.0000i
   1.2681 + 0.0000i
   0.0797 + 0.9098i
   0.0797 - 0.9098i

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章