MATLAB之微积分

数值微积分

1.polyval()---建立多项式
>> a = [1,0,0];----------f = x^2,用列表a来表示多项式的系数
>> x = -100:0.01:100;----用x来表示自变量
>> f = polyval(a, x);----创建多项式f(x)
>> plot(x, f);------画图
​
2.polyder(x)--对多项式求导,x表示多项式的系数
>> polyder(a)------f(x) = x^2,求导后f‘(x)=2x
ans =
     2     0
​
>> polyval(polyder(a),2)-----求出f‘(2)
ans =
     4
​
3.conv()做多项式乘法,deconv()做多项式除法
f(x) = (20x^3 - 7x^2 + 5x + 10)(4x^2 + 12x -3)     -2<=x<=1
>>  a = [20 -7 5 10];
    b = [4 12 -3];
    c = conv(a,b);
    x = -2:0.1:1;
    f1 = polyval(c,x);
    f2 = polyval(polyder(c),x);
    plot(x,f1,'-b',x,f2,'r');
    legend('f(x)','f`(x)') 
    
​
4.polyint(x,k)--对多项式求积分,x表示多项式的系数,k表示积分后的常数项
>> a = [5 0 -2 0 1];
>> polyint(a,3);
>> polyint(a,3)
ans =
    1.0000         0   -0.6667         0    1.0000    3.0000
    
>> polyval(polyint(a,3),7)
ans =
   1.6588e+04
​
​
5.对曲线某个点求导, f'(x) = (f(x0+h)-f(x0))/h --- h->0
diff用来算之间的差 --- 后面的数减去前面的数 
>> a = [5 0 -2 0 1];
>> diff(a)
ans =
    -5    -2     2     1
    
>> x0 = pi/2; h = 0.1;
x = [x0 x0+h];
y = [sin(x0) sin(x0+h)];
m = diff(y)./diff(x)
m =
   -0.0500
​
6.求f'(x),f''(x)
>>  x = -2:0.005:2;
    y = x.^3;
    m = diff(y)./diff(x);
    m2 = diff(m)./diff(x(1:end-1));
    plot(x,y,x(1:end-1),m)
    plot(x,y,x(1:end-1),m,x(1:end-2),m2)
    
7.求曲线在坐标轴上所围成的面积
​
midpoint--用矩形来填充曲线所围成的面积
>>  h = 0.005; x = 0:h:2;
    midpoint = (x(1:end-1)+x(2:end))./2;---寻找每个矩形的中心点
    y = 4*midpoint.^3;
    s = sum(h*y)
    s =
       16.0000
   
trapz--用梯形来填充曲线所围成的面积
>>  h = 0.05; x = 0:h:2; y = 4*x.^3;
    s = h*trapz(y)
    s =
       16.0100
​
8.@ -- 类似于匿名函数的东西,前面是变量,后面是函数体。
9.integral 一重积分
  integral2 二重积分
  integral3 三重积分
​
>>  y = @(x) 1./(x.^3-2*x-5);
    integral(y,0,2)
    ans =
       -0.4605
       
>>  f = @(x,y) y.*sin(x)+x.*cos(y);
    integral2(f,pi,2*pi,0,pi)
    ans =
       -9.8696
       
>>  f = @(x,y,z) y.*sin(x)+z.*cos(y);
    integral3(f,0,pi,0,1,-1,1)
    ans =
        2.0000

猜你喜欢

转载自blog.csdn.net/qq_41458842/article/details/101367632