?部分效果图:
插值算法
?
曲线拟合:
?
非线性最小二乘拟合:
?
?
?
?代码实现:
%collect按照一定的变量展开
clear
clc
syms x t
EXPR = str2sym('(x^2+x*exp(-t)+1)*(x+exp(-t))')
expr1 = collect(EXPR)
expr2 = collect(EXPR,exp(-t))
expr3 = collect(EXPR,[x,exp(-t)])
f = expand(sin(x+3*t))
coeffs_cosy = collect(f,cos(t))
coeffs_mat = collect ([(x+1)*(t+1),x^2+x*(x-t);2*x*t-x,x*t+x/t],t)
clear
clc
%%collect函数展开
syms a b c d e f
F = a*sin(2*x) + c*cos(x) + d*cos(x) + e*sin(3*x) + f*sin(3*x)
coeffs_sin = collect(F,'sin')
coeffs_sincos = collect(F,{'sin','cos'})
%因式分解
clear
clc
syms a x
f1 = x^4-5*x^3+5*x^2+5*x-6
fac1 = factor(f1)
f2 = x^2-a^2
fac2 = factor(f2)
fac3 = factor(1025)
%多项式或者函数分解
clear
clc
syms x y
expand((x+1)^3)
expand(sin(x+y))
%化简
clear
clc
syms x a
f = (1/x^3+6/x^2+12/x+8)^(1/3)
%sfy1 = simplify(f,'full')
sfy2 = simplify(f)
%sfy3 = simplify(f,'class')
ff = cos(x)+sqrt(-sin(x)^2)
ssfy1 = simplify(ff)
ssfy2 = simplify(ssfy1)
%变量替换
clear
clc
syms a x
f = a*sin(x)+5
f1 = subs(f,sin(x),sym('y'))
f2 = subs(f,{a,x},{2,sym(pi/3)})
f3 = subs(f,{a,x},{2,pi/3})
f4 = subs(subs(f,a,2),x,0:pi/6:pi)
f5 = subs(f,{a,x},{0:6,0:pi/6:pi})
%求极限
clear
clc
syms x
f = sin(x)/x
val1 = limit(f,x,0)
f1 = (1+4*x)^(1/x)
f2 = (exp(x)-1)/x
val2 = limit(f1,x,0)
val3 = limit(f2,x,0)
f3 = sqrt(x)-2^(-1/x)
limit(f3,x,0,'right')
%求导
clear
clc
syms x
f = 3*x^3+5*x+1
%%一元函数求导
%二阶导
danhsu1 = diff(f,2)
%导数值
f1 = 3*x^3-2*x+1
daoshu2 = diff(f1)
x = 1
eval(daoshu2)
%%二元函数求导
syms a b
z = a^2*sin(2*b)
B = diff(z,a)
A = diff(z,b)
C = diff(z,a,b)
%%积分运算
%一元函数不定积分
clear
clc
syms x
y = 1/(sin(x)^2*cos(x)^2)
A = int(y)
pretty(int(y))
syms z
B = int(x/(1+z^2),z)
%%一元函数定积分
clear
clc
syms x
y = (x^2+sin(x))/(1+x^2)
int(y,x,-1,1)
%多重积分
clear
clc
syms x y
int(int(x^2+y^2+1,y,x,x+1),x,0,1)
%%Taylor展开
clear
clc
syms x
f = x*atan(x)-log(sqrt(1+x^2))
taylor(f)
T1 = taylor(exp(x))
f1 = 1/x^2
taylor(f1,x, 'ExpansionPoint',2,'Order',6)
pretty(taylor(f1,x,2))
%%数值方法
%用复合梯形公式和符合辛普森公式求定积分
clear
clc
syms x
x = 2:0.1:5
y = log(x)./(x.^2)
t = trapz(x,y)
ff = @(x) log(x)./(x.^2)
q = integral(ff,2,5)
syms i1
f=log(i1)/(i1^2)
eval(int(f,i1,2,5))
%%数值方法求二重积分
clc
clear
syms x y
%x = 2:0.1:4
%y = 0:0.1:1
z =@(x,y)x.*y
Q = integral2(z,0,4,0,1)
%%线性方程组与非线性方程组求解
clear
clc
s1 = str2sym('x^3-x^2+9*x-10')
solve(s1)
%%求超越方程的根
clear
clc
syms x y
ff = str2sym('p*sin(x)=r')
solve(ff,x)
eqns = [x+y==1, x-11*y==5];
S = solve(eqns, [x y], 'ReturnConditions', true);
S.x
S.y
S.conditions
S.parameters
%%求和与求极值
clear
clc
a = 1:5
A = [1 2 3;2 3 4;7 8 9]
sum(a)
sum(A)
%%级数求和
clear
clc
syms k n
f = k^3
symsum(f,k,0,n-1)
syms x p
symsum(x^p/factorial(p),p,0,inf)
%%求函数极值点
%一元函数
clear
clc
syms x
y = @(x)sin(x)+3
x = fminbnd(y,2,5)
%%函数插值与曲线拟合
clear
clc
x0 = -5:0.5:5
y0 = 1./(1+x0.^2)
x = -5:0.2:5
y1 = interp1(x0,y0,x,'linear')
y2 = interp1(x0,y0,x,'spline')
y3 = interp1(x0,y0,x,'nearest')
subplot(2,2,1),plot(x0,y0,'r-p'),title('y=1/(1+x^2)')
subplot(2,2,2),plot(x0,y0,'r-',x,y1),title('linear')
subplot(2,2,3),plot(x0,y0,'r-',x,y2),title('spline')
subplot(2,2,4),plot(x0,y0,'r-',x,y3),title('nearst')
%%二维插值
%%曲线拟合
clear
clc
clf
x = 0:0.1:1
y = [2.3,2.5,2.1,2.5,3.2,3.6,3.0,3.1,4.1,5.1,3.8]
p2 = polyfit(x,y,2)
p3 = polyfit(x,y,3)
p7 = polyfit(x,y,7)
disp('二次拟合曲线'),p2
disp('三次拟合曲线'),p3
disp('七次拟合曲线'),p7
x1 = 0:0.1:1
y2 = polyval(p2,x1)
y3 = polyval(p3,x1)
y7 = polyval(p7,x1)
plot(x,y,'rp',x1,y2,'--',x1,y3,'k-',x1,y7)
legend('拟合点','二次拟合','三次拟合曲线','七次拟合')
%%非线性最小二乘拟合
clear
clc
x0=[10,0.5]
x = lsqnonlin('ct',x0)
tt=0:0.2:8
yy=x(1).*exp(-x(2).*tt)
plot(tt,yy,'-rp')
|