1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
| % 简答与编程三 数据可视化 % Test 1 % 分别绘图 % sin(x) x1=linspace(0,4*pi,2000); plot(x1,sin(x1))
% cos(x) x2=linspace(-pi,3*pi,2000); plot(x2,cos(x2))
% tan(x) x3=linspace(0,4*pi,2000); plot(x3,tan(x3))
% 绘制在一幅图中 subplot(131); plot(x1,sin(x1)) subplot(132); plot(x2,cos(x2)) subplot(133); plot(x3,tan(x3))
% Test 2 % sin(x) x1=linspace(0,4*pi,2000); plot(x1,sin(x1),'k') axis auto; hold on;
% cos(x) x2=linspace(-pi,3*pi,2000); plot(x2,cos(x2),'--g*','linewidth',3) hold on;
% tan(x) x3=linspace(0,4*pi,2000); plot(x3,tan(x3),'-.ro','linewidth',2);
% 图例标注 legend('sin(x)','cos(x)','tan(x)','4');
% Test3 x=linspace(-3,3,49); y=linspace(-3,3,49); [xx,yy]=meshgrid(x,y); %确定作图的范围 zz=xx.^2/8-yy.^2/3;
% 使用mesh函数 mesh(xx,yy,zz)
% 使用surf函数 surf(xx,yy,zz)
% Test4
% sin(x) x1=linspace(0,4*pi,2000); p1=plot(x1,sin(x1)); hold on; % cos(x) x2=linspace(-pi,3*pi,2000); p2=plot(x2,cos(x2));
% 获取图形句柄并修改曲线属性 h1=findobj(p1); set(h1,'color','r') set(h1,'linewidth',2) h2=findobj(p2); set(h2,'color','g','linestyle','--')
% 简答与编程五 程序设计 % Test 01 % swith score1 = input('Please enter your score1:'); switch score1 case num2cell(90:100) disp('A') case num2cell(80:89) disp('B') case num2cell(70:79) disp('C') case num2cell(60:69) disp('D') case num2cell(0:59) disp('E') otherwise error('Inputting Error!') end
% if score2 = input('Please enter your score2:'); if (score2 >= 90 & score2 <= 100) disp('A') elseif (score2 >= 80 & score2 <= 89) disp('B') elseif (score2 >= 70 & score2 <= 79) disp('C') elseif (score2 >= 60 & score2 <= 69) disp('D') elseif (score2 >= 0 & score2 <= 59) disp('E') else error('Inputting Error!') end
% Test 02 % 数值计算:计算出0-1000之间所有是4的倍数的整数的和 % for循环 s1=0; for i = 0:4:1000 s1=s1+i; end s1
% while 循环 num=0; s2=0; while num<=1000 s2=s2+num; num=num+4; end s2 % 不使用循环语句 s3=sum(0:4:1000) % 显然s1,s2,s3的值都是相等的
% 符号计算 % 计算出 1+1/2^2+1/3^2+...+1/k^2+...的前10项和
% for 循环 s4=0; for j = 1:10 s4=s4+1/j^2; end s4 % while 循环 s5 = 0; num1=1; while num1<=10 s5=s5+1/num1^2; num1=num1+1; end s5 % 不使用循环语句 syms k s6=symsum(1/k^2,1,10)
% Test 02'!!!! % for 循环 s1= 0; for i = 0:10000001; s1=s1+0.2^i; end s1
% while循环 num=0; s2=0; while num<=1000000 s2=s2+0.2^num; num=num+1; end s2
% 很显然s1和s2是相等的。
% Test 03 function [] = Test(varargin) % Test 给定不同的输入绘画出不同的图形 % 当没有输入量时,画出单位圆 % 当输入量是大于2的整数N时,绘制正N边形并在图片名中反映显示多边形的真实边数 % 当输入量是"非自然数数"时,给出"出错提示"。 % Author:雷博闻 % Time: 2020/03/25 if nargin == 0 angle=0:pi/360:2*pi; figure(1); plot(1*sin(angle),1*cos(angle)); axis tight axis equal
elseif nargin ==1 a = varargin{1}; if a > 2 & rem(a,1)==0 pgon=nsidedpoly(a); figure(1); plot(pgon) title(['正',num2str(a),'边形']) axis equal axis tight
elseif a<0 | (a>=0 & rem(a,1)~=0) error('请输入一个自然数!') else error('超出范围') end end
% Test 04 n=[2:100]; number=0; tem=0; sum=0; for i=1:length(n)-1 tem=n(i)*n(i+1)-1; for j=2:floor(tem/2) if rem(tem,j)~=0 if j==floor(tem/2) disp([num2str(n(i)),'和',num2str(n(i + 1)),'是一对亲密数']) number=number+1; sum=sum+tem; end else break; end end end
number sum
% 简答与编程5 数值计算 % Test1 function [] =Test A = [27 6 -1;6 15 2;1 1 54]; b = [85;5;110];
% LU 分解法 [L,U] = lu(A); X = U\(L\b)
% 雅可比迭代法 [x1,n1]=jacobi(A,b, [0;0;0],1.0e-3)
% G-S迭代法 [x2,n2]=gauseidel(A,b, [0;0;0],1.0e-3)
function [y,n] = jacobi(A,b,x0,ep) % 雅可比迭代法算法 D = diag(diag(A)); L = -tril(A,-1); U=-triu(A,1); B=D\(L+U); f=D\b; y=B*x0+f; n=1; while norm(y-x0) >= ep x0=y; y=B*x0+f; n=n+1; end
function [y,n]=gauseidel(A,b,x0,ep) D = diag(diag(A)); L = -tril(A,-1); U=-triu(A,1); B=(D-L)\U; f=(D-L)\b; y=B*x0+f; n=1; while norm(y-x0) >= ep x0=y; y=B*x0+f; n=n+1; end
% Test 2
function [] = Test() % 主函数 % solve 命令求解 syms x answer=solve(x.^3 - 3*x+1); vpa(answer)
% 实现逐步搜索法求方程的解 tic X=StepSearch(-10,10,0.001,0.0001) toc % 实现二分法 tic [k1,x1,wuca1,yx1]=erfen(-2,-1,0.0001); x1 [k2,x2,wuca2,yx2]=erfen(0,1,0.0001); x2 [k3,x3,wuca3,yx3]=erfen(1,2,0.0001); x3 toc
function y = funs(x)
y = x.^3 - 3 * x + 1;
function r=StepSearch(a,b,h,tol) % 逐步搜索法算法 X=a:h:b;n=(b-a)/h+1;m=0; Y=funs(X); X(n+1)=X(n);Y(n+1)=Y(n); k=2; while(k<=n) sk=Y(k)*Y(k-1); if sk<=0 m=m+1; if(abs(Y(k))<abs(Y(k-1))) r(m)=X(k); k=k+1; else r(m)=X(k-1); end else xielv=(Y(k+1)-Y(k))* (Y(k)-Y(k-1)); if (abs(Y(k))<tol)&( xielv<=0) m=m+1;r(m)=X(k); end end k=k+1; end
function [k,x,wuca,yx]=erfen(a,b,abtol) % 二分法算法 a(1)=a; b(1)=b;
ya=funs(a(1)); yb=funs(b(1)); %程序中调用的fun.m为函数
if ya* yb>0
disp('注意:ya*yb>0,请重新调整区间端点a和b.'), return end max1=-1+ceil((log(b-a)- log(abtol))/log(2)); %ceil是向正方向取整 for k=1:max1+1
ya=funs(a); b; yb=funs(b); x=(a+b)/2;
yx=funs(x); wuca=abs(b-a)/2; k=k-1; [k,a,b,x,wuca,ya,yb,yx];
if yx == 0 a=x;b=x; elseif yb*yx>0 b=x;yb=yx; else a=x;ya=yx; end if b-a<abtol,return,end end k=max1;x;wuca;yx=funs(x);
% 简答与编程5 数值计算 % Test3 x=linspace(-5,5,10); % 已知数据点的x坐标 y=1./(1+x.^2); % 已知数据点y坐标 x0=linspace(-5,5,10); % 已知数据点x坐标 y0=1./(1+x0.^2); % 已知数据点y坐标 x1=linspace(-5,5,10); y1=interp1(x,y,x1,'linear'); % 线性插值 y2=interp1(x,y,x1,'spline'); % 三次样条插值 y3=interp1(x,y,x1,'pchip'); % 三次Hermite插值 y4=interp1(x,y,x1,'nearest'); % 最邻近插值
% 作图 figure(1) plot(x0,y0,'-',x,y,'og',x1,y1,'*r'); legend('被插值曲线','已知离散数据点','线性插值数据点 ','location','NorthWest'); title('interp with linear') figure(2) plot(x0,y0,'-',x,y,'og',x1,y4,'hr'); legend('被插值曲线','已知离散数据点','线性插值数据点 ','location','NorthWest'); title('interp with cubic') figure(3) plot(x0,y0,'-',x,y,'og',x1,y2,'sr'); legend('被插值曲线','已知离散数据点','线性插值数据点 ','location','NorthWest'); title('interp with spline') figure(4) plot(x0,y0,'-',x,y,'og',x1,y3,'dr'); legend('被插值曲线','已知离散数据点','线性插值数据点 ','location','NorthWest'); title('interp with pchip')
% 简答与编程5 数值计算 % Test4 x=1.23; h=[0.1,0.01,0.001,0.0001]; x1=x+h; x2=x-h; y = 7.*x.*(sin(3.*x)+cos(3.*x)); y1= 7.*x1.*(sin(3.*x1)+cos(3.*x1)); y2=7.*x2.*(sin(3.*x2)+cos(3.*x2)); yCS=(y1-y2)./(2*h) % 差商数值求导 syms x; f=7.*x.*(sin(3.*x)+cos(3.*x)); dy=diff(f,x); % 理论导数符号表达式 dyl=eval(subs(dy,x,1.23)) ;% 理论导数 detF=yCS-dyl % 差商求导误差
% Test 05
h=(exp(1)-1)/20; x=1:h:exp(1); y=2.*x.^3.*log(5*x); z1=sum(y(1:20))*h; z2=sum(y(2:21)*h); z=(z1+z2)/2,z3=trapz(y)*h,z3h=trapz(x,y)
|