Matlab %陆
阅读原文时间:2023年07月08日阅读:1

第六章 MATLAB IN ENGINEERING

 %幂级数
 f(x) = x^3-2x-5;
 p = [1 0 -2 -5]  %自变量前系数
 ​
 polyval()
 eg:
 a = [9, -5, 3, 7];
 x = -2:0.01:5;
 f = polyval(a,x);  %表示a系数下的X处的微商数值
 plot(x,f,'LineWidth',2);
 xlabel('x');
 ylabel('f(x)');
 set(gca,'FontSize',14)
 ​
 polyder()  %给的是微商式子
 eg:
 p = [5 0 -2 0 1];
 polyder(p)    %微分符号
 eg:
 p = [5 0 -2 0 1];
 polyder(p);
 polyval(polyder(p),7)

 polyint()  %不定积分
 eg:
 p = [5 0 -2 0 1];
 polyint(p,3);    %积分后的常数项是3
 polyval(polyint(p,3),7)    %不定积分7处的积分值

 diff()    %计算前后差异  
 eg1:
 x = [1 2 5 2 1];
 diff(x)   % ans = [1 3 -3 -1];
 eg2(两点斜率(1,5),(2,7)):
 x = [1 2];
 y = [5 7];
 slope = diff(y)./diff(x)
 ​
 eg3:
 x0 = pi/2;
 h = 0.1;
 x = [x0 x0+h];
 y = [sin(x0) sin(x0+h)];
 m = diff(y)./diff(x)
 eg4:
 h = 0.5;
 x = 0:h:2*pi;
 y = sin(x);
 m = diff(y)./diff(x);

 //h的大小对于导函数的影响:
 g = colormap(lines);
 hold on;
 for i = 1:4
  x = 0:power(10,-i):pi;
  y = sin(x);
  m = diff(y)./diff(x);
  plot(x(1:end-1),m,'Color',g(i,:));
 end
 hold off;
 set(gca,'XLim',[0,pi/2]);
 set(gca,'YLim',[0,1.2]);
 set(gca,'FontSize',18);
 set(gca,'FontName','symbol');
 set(gca,'XTick',0:pi/4:pi/2);
 set(gca,'XTickLabel',{'0','p/4','p/2'});
 h = legend('h = 0.1','h = 0.01','h = 0.001','h = 0.0001');
 set(h,'FontName','Times New Roman');
 box on;

 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,x(1:end-2),m2);
 xlabel('x','FontSize',18);
 ylabel('y','FontSize',18);
 legend('f(x) = x^3','f''(x)','f''''(x)');
 set(gca, 'FontSize',18);

Midpoint Rule用矩形去逼近

 h = 0.05;
 x = 0:h:2;
 midpoint = (x(1:end-1)+x(2:end))./2;
 y = 4*midpoint.^3;   %f(x) = 4*x^3;
 s = sum(h*y)

Trapezoid Rule用梯形去逼近

 trapz();%梯形数值积分
 eg:
 h = 0.05;
 x = 0:h:2;
 y = 4*x.^3;
 s = h*trapz(y);  
 OR:
 h = 0.05;
 x = 0:h:2;
 y = 4*x.^3;
 trapezoid = (y(1:end-1)+y(2:end))/2;
 s = h*sum(trapezoid);

Simpson's Rule

 h = 0.05;
 x = 0:h:2;
 y = 4*x.^3;
 s = h/3*(y(1)+2*sum(y(3:2:end-2))+4*sum(y(2:2:end))+y(end))

function [y] = xy_plot(input,x) %丢入类似@sin ; 一个函数不能作为input
% xy_plot receives the handle of a function and plots that
%function of x
y = input(x);
plot(x,y,'r--');
xlabel('x');
ylabel('function(x)');
end

integral();
eg:
y = @(x) 1/(x.^3-2*x-5);
integral(y,0,2) %0,2是上下限

eg1:
f = @(x,y) y.*sin(x)+x.*cos(y);
integral2(f,pi,2*pi,0,pi) %二重积分,顺序跟书写的一样

eg2:
f = @(x,y,z) y.*sin(x)+z.*cos(y);
integral3(f,0,pi,0,1,-1,1);

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章