structstate { int resource[m]; // 系统中每种资源的总量,向量R int avalaible[m]; // 未分配给进程的每种资源的总量,向量V int claim[n][m]; // 进程i对资源j的最大需求,矩阵C int allocation[n][m]; // 系统为进程i分配资源j的数量,矩阵A }
functionres=my_simpson(f,a,b) res=(b-a)*(f(a)+f(b)+4*f((a+b)/2))/6; end
functionres=my_quad(f,a,b,tol) if nargin==3 tol=10^(-6); end mid=(a+b)/2; quad_all=my_simpson(f,a,b); quad_l=my_simpson(f,a,mid); quad_r=my_simpson(f,mid,b); if(abs(quad_l+quad_r-quad_all)<15*tol) res=quad_l+quad_r+(quad_l+quad_r-quad_all)/15; else res=my_quad(f,a,mid,tol)+my_quad(f,mid,b,tol); end end
import numpy as np import sympy import matplotlib.pyplot as plt
x = sympy.Symbol('x') y = sympy.sin(x) n = [11, 14, 17] val = [] cor = ['r', 'k', 'b', 'c'] xp = np.linspace(-6, 6, 400) funco = sympy.lambdify(x, y, 'numpy') yo = funco(xp) val.append(yo)
for i inrange(len(n)): fi = y.series(x, 0, n[i]).removeO() funci = sympy.lambdify(x, fi, 'numpy') yi = funci(xp) val.append(yi)
fig = plt.figure() ax = fig.add_subplot(1, 1, 1) for i inrange(len(val)): ax.plot(xp, val[i], color = cor[i]) plt.show()
T=2*pi; fx=@(x) (mod(x+pi,T)<pi).*(T-mod(x,T)).^2; test=4; xp=linspace(-2*pi,2*pi,1000); N=[2,4,8,16]; col=['red','cyan','blue','yellow']; idx=2; yp=fx(xp); plot(xp,yp,'color','red','LineWidth',2,'DisplayName','OriginSignal') hold on yp_test=[]; fori=1:test n=N(i); [fs,an,bn]=FourierSeries(fx,x,n); yp_t=subs(fs,x,xp); yp_t=double(yp_t); yp_test=[yp_test;[yp_t]]; end plot(xp,yp_test(1,:),'color','green','LineWidth',2,'DisplayName','n=2'); hold on; plot(xp,yp_test(2,:),'color','cyan','LineWidth',2,'DisplayName','n=4'); hold on plot(xp,yp_test(3,:),'color','yellow','LineWidth',2,'DisplayName','n=8'); hold on; plot(xp,yp_test(4,:),'color','blue','LineWidth',2,'DisplayName','n=16'); hold on; legend('show','location', 'northeast'); hold off
function[fs,an,bn]=FourierSeries(fx,x,n,l,r) %fx为周期信号 %x为自变量 %n为展开阶数 %[l,r]为周期区间 %an,bn为余弦项系数,正弦项系数 %fs为展开表达式 if nargin==3 l=-pi; r=pi; end T=r-l; w=2*pi/T; %fx=subs(fx,x,x+l+T/2); fx=@(x)fx(x+l+T/2); an=integral(fx,-T/2,T/2)/T; bn=[]; fs=an; for k=1:n ak=integral(@(x) fx(x).*cos(k*w*x),-T/2,T/2)*2/T; bk=integral(@(x) fx(x).*sin(k*w*x),-T/2,T/2)*2/T; an=[an,ak]; bn=[bn,bk]; fs=fs+ak*cos(k*w*x)+bk*sin(k*w*x); end fs=subs(fs,x,x-l-T/2); end
import numpy as np import matplotlib.pyplot as plt from scipy.integrate import quad
# Define the original signal function fx T = 2 * np.pi deffx(x): return (np.mod(x + np.pi, T) < np.pi) * (T - np.mod(x, T)) ** 2
# Fourier series calculation deffourier_series(fx, x, n, l=-np.pi, r=np.pi): T = r - l w = 2 * np.pi / T fs = 0 an = [] # Cosine coefficients bn = [] # Sine coefficients
# Calculate remaining an and bn coefficients for k inrange(1, n + 1): ak, _ = quad(lambda x: fx(x) * np.cos(k * w * x), -T/2, T/2) bk, _ = quad(lambda x: fx(x) * np.sin(k * w * x), -T/2, T/2) an.append(ak * 2 / T) bn.append(bk * 2 / T) fs += an[k] * np.cos(k * w * x) + bn[k] * np.sin(k * w * x)
return fs, an, bn
# Define the range of x and the number of Fourier series terms test = 4 xp = np.linspace(-2*np.pi, 2*np.pi, 1000) N = [2, 4, 8, 16] colors = ['green', 'cyan', 'yellow', 'blue']
# Plot the original signal yp = fx(xp) plt.plot(xp, yp, color='red', linewidth=2, label='OriginSignal')
# Plot the Fourier approximations for idx, n inenumerate(N): fs, _, _ = fourier_series(fx, xp, n) plt.plot(xp, fs, color=colors[idx], linewidth=2, label=f'n={n}')
# Show the legend and plot plt.legend(loc='northeast') plt.show()
x =[49121]; y =[18551]; t1=x>=2% t1 = 1 1 0 1 0 u=x-y % u = 3 1 -4 -3 0 t2=x-y>=1%t2 = 1 1 0 0 0
a=3; b=7; if a>3 & b>3, disp('handle 1') else disp('handle 2')%输出 end
数据类型
主要的数据类型:double char sym struct cell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
a=rand(3); b='Li San'; %double型,char型 symsx, y=1 + x^2%x,y为sym类型;对y赋值的语句含符号对象 F.name='li San', F.birth=1999, F.src=rand(3) %F为struct型 whos a b x y F class(a) % 'double' class(b) % 'char' %whos结果 Name Size Bytes Class Attributes