matlab 交流群问题解决思路及参考源码:

代码和思路都是凭感觉 写的,希望起到抛砖引玉的作用


问题1:输入方差、均值画出正态分布图

matlab源码:

clear,clc;
mu=input('请输入mu:');
sigma=input('请输入sigma:');
x=mu-sigma*3.5:0.1:mu+sigma*3.5;
y=1/(sqrt(2*pi*sigma)).*exp(-(x-mu).^2/(2*sigma^2));
plot(x,y,'-r')
hold on
plot([mu  mu],[0,max(y)],':k')
title(['\mu=',num2str(mu),'    \sigma^2=',num2str(sigma^2)])
grid minor
axis([mu-sigma*3 mu+sigma*3 0 max(y)*1.08])
set(gca,'box','off')

 

python源码:

import  numpy as np
from matplotlib import pyplot as plt
mu=eval(input('请输入mu:'))
sigma=eval(input('请输入sigma:'))
x=np.linspace(mu-sigma*3.5,mu+sigma*3.5,100)
y=1/(np.sqrt(2*np.pi*sigma))*np.exp(-(x-mu)**2/(2*sigma**2))
plt.plot(x,y,'-r')
plt.grid(':')
plt.title(r'$\mu=$'+str(mu)+' $\sigma^2=$'+str(sigma**2))
plt.xlim(mu-sigma*3,mu+sigma*3)
plt.ylim(0,max(y)*1.08)

 

问题2:怎么画三维波形图

matlab源码:

clear,clc
t = 0:pi/20:8*pi;
for i=1:3
    eval(['x',num2str(i),'=i+zeros(1,length(t));']);
    eval(['y',num2str(i),'=','i*cos(i*t);']);
    plot3(eval(['x',int2str(i)]),t,eval(['y',int2str(i)]))
    hold on
end
xlabel('x'),ylabel('y'),zlabel('z','rotation',0)
axis([0 4 0 8*pi*1.1 -3 3])
grid minor

t=1:1000;
y=zeros(1000,20);
y(t<500,1)=1;
y(t>500,1)=-1;
plot3(t,ones(1000,1),y(:,1),'linewidth',3);
hold on;
h=fft(y(:,1))/500;
for m0=1:19
    y(:,m0+1)=abs(h(m0*2))*cos((m0*2-1)*t/1000*2*pi+angle(h(m0*2)));
    plot3(t,(m0+1)*ones(1000,1),y(:,m0+1));
end
grid minor

 

python源码

import numpy as np
from matplotlib import pyplot as plt
ax1 = plt.axes(projection='3d')
x=np.linspace(0,8*np.pi,200)
y1=np.ones(len(x))
z1=2*np.cos(2*x)+2
z2=2*np.sin(x)+2
plt.xlabel('x')
plt.ylabel('y')
ax1.plot3D(x,y1,z1,'r-')    #绘制空间曲线
ax1.plot3D(x,y1-2,z2,'g-')    #绘制空间曲线
plt.ylim(-2,2)
plt.show()

问题3 :怎么去掉x轴

怎么把x轴去掉

思路:x轴改成 白色

matlab源码:

clear,clc
x=(1:1:4);
y1=[0.362 0.313 0.314 0.402];
y2=[-0.0364 -0.0338 -0.0329 -0.0444];
e1=[0.01606 0.00825 0.00058 0.00068];
e2=[0.00034 0.00031 0.00076 0.00008];
bar(x,y1,0.5,'facecolor',[0.7,0.7,0.7],'BaseValue',0);
errorbar(x,y1,e1,'.k');
bar(x,y2,0.5,'facecolor',[0.7,0.7,0.7]);
errorbar(x,y2,e2,'.k');
axis([0 5 -0.1 0.5]);
set(gca,'xtick',[1:1:4],'XColor','white');
set(gca,'xticklabel',{'CK','T1','T2','T3'});
set(gca,'xticklabel',[]);
hold off

问题4:怎么画示波器图像

matlab 源码

clear
clc
x=0:0.01:10*pi;
for i=1:length(x)
y1(i)=sin(pi/10*x(i))+3+0.05*(randn(1,1)-1);
y2(i)=cos(pi/10*x(i))+3+0.06*(randn(1,1)-1);
end
plot(x,y1,x,y2)
axis([0 10*pi 1.5 5])

 

python源码

from matplotlib import pyplot as plt
import numpy as np
x=np.linspace(-np.pi,10*np.pi,500)
y1=np.ones(len(x))
y2=np.ones(len(x))
for i in np.arange(0,len(x)):
    y1[i] = np.sin(np.pi /10 * x[i]) + 3 + 0.1 * (np.random.rand()- 1)
    y2[i] = np.cos(np.pi / 10 * x[i]) + 3 + 0.2 * (np.random.rand() - 1)
plt.plot(x,y1)
plt.plot(x,y2)
plt.grid(c='k',linestyle=':')
plt.show()

 

 问题5:解方程

 matlab源码

clear,clc;
num=0;
for x=1:100
    for y=1:100
        for z=1:100
                if (x+y+z)*(1/x+1/y+1/z)==14
                    num=num+1;
                    disp(['第',num2str(num),'组合为:x=',num2str(x),'  y=',num2str(y),'  z=',num2str(z)])
                end
        end
    end
end
%[2K,3K,10K]
%[3K,10K,15K]

问题6 :怎么画散点热力图

clear,clc
x = linspace(0,3*pi,200);
y = x + 5*rand(1,200);
size = 45;
color =1:length(x);
scatter(x,y,size,color,'filled')
grid minor
colorbar
colormap(hot)
set(gca,'box','on')

 

 问题7.怎么画凸包络图

举个例子

x=rand(1,20);
y=rand(1,20);
z=rand(1,20);
k=convhull(x,y,z);
h=trisurf(k,x,y,z);
hold on
plot3(x,y,z,'+r')
h.FaceAlpha=0.5;

 

dt=1e-6;
T=2*1e-3;
for N=0:500
    t=N*T+(0:dt:T);
    input=2*cos(2*pi*1005*t);
    carrier=5*cos(2*pi*(1e4)*t+0.1*randn);
    output=(2+0.5*input).*carrier;
    nosie=randn(size(t));
    r=nosie+output;
    subplot(3,1,1);plot([0:dt:T],input);
    subplot(3,1,2);plot([0:dt:T],carrier);
    subplot(3,1,3);plot([0:dt:T],r);
    set(gcf,'DoubleBuffer','on');
    drawnow;
end

问题:8、怎么画正视图

 然后我要了他的代码略微修改

clc
clear
close all

N=128;%设置取样点数
lamda=632.8e-9;%波长设置     
w0=1.5e-3;%高斯光束的束腰
a=5e-3;%元件宽度
L=280;
x=linspace(-a/2,a/2,N);
y=x;
[X,Y]=meshgrid(x,y);
rr2=X.^2+Y.^2;
F=exp(-rr2./w0^2);
figure
mesh(x,y,F)
axis([-1.2/2*a 1.2/2*a -1.2/2*a 1.2/2*a 0 1])
title('入射高斯光束的三围光强分布')

%%%%%%%%%%%%%%%%%选择整形图形%%%%%%%%%%%%%%%%%%%
G=zeros(N,N);
G(N/2-15:N/2+15,N/2-15:N/2+15)=1;%正方形
%G(sqrt(X.^2+Y.^2)<a/3)=1;%圆形
%G(Y>-1e-3&X+Y<1e-3&Y-X<1e-3)=1;%直角三角形
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Iout=sqrt(sum(sum(F))^2/sum(sum(G)));%输出光强
GG=zeros(N,N);
GG(N/2-15:N/2+15,N/2-15:N/2+15)=Iout;
%GG(sqrt(X.^2+Y.^2)<a/3)=Iout;
figure
imshow(G)
title('整形图形')
%%%%%%%%%%%%%%%%初始相位设置%%%%%%%%%%%%%%%%%%%
fai{1}=zeros(N,N);
%fai{1}=2*pi.*rand(N,N)-pi;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

p=5000;%迭代次数设置
sn=50;
beta=1000;
for n=1:p
    if n>sn+1
        f=F.*exp(i.*fai{n});
        g=fft2(f);
        psai=angle(g);
        gg=G.*exp(i.*psai);
        ff=ifft2(gg);
        if sum(SSE(n-sn:n-1))/sn-SSE(n-1)<0.000001
            nn1=ceil(rand*(n-1));%随机取出一个相位
            nn2=ceil(rand*(n-1));
            fai{n+1}=beta.*fai{nn1}+(1-beta).*fai{nn2};
        else
            fai{n+1}=angle(ff);
        end
    else
        f=F.*exp(i.*fai{n});
        g=fft2(f);
        psai=angle(g);
        gg=G.*exp(i.*psai);
        ff=ifft2(gg);
        fai{n+1}=angle(ff);
    end
    ggg=abs(fft2(F.*exp(i.*fai{n+1})));
    %ggg=ggg./2;%max(max(ggg));
    SSE(n)=sum(sum((ggg-GG).^2))/sum(sum(GG.^2));
    yita(n)=sum(sum(ggg.*GG))./sqrt(sum(sum(ggg.^2))*sum(sum(GG.^2)));
end
%figure
%imshow(ggg)
%ggg=abs(fft2(F.*exp(i.*fai{find(SSE==min(SSE))+1})));
figure
mesh(x,y,ggg)
axis([-3e-3 3e-3 -3e-3 3e-3 0 250])
title('模拟图形')
figure
plot(1:p,SSE)
title('均方误差')
figure
plot(1:p,yita);
title('拟合系数')
figure('name','模拟图的切面图')
ggg_y=max(ggg);
plot(ggg_y)
grid minor

问题9、这么求矩阵中连续的1

clear
clc
[Num,Txt,Raw]=xlsread('test.xlsx');
x=[0;Num;0;1];
idx=find(x==1);
start=find(diff(x)==1)+1;
finish=idx(diff(idx)~=1);
for i=1:length(finish)
    data{i}=x(start(i):finish(i));
end
for j=1:length(data)
    len(j)=length(data{j});
end
tabulate(len)



a=[1 ...
    3 4 5 ...
    7 8 9 10 11 12 13 ...
    21 22 23 24 25];
inter=find(diff(a)~=1);
for i=1:length(inter)+1
    if i==1
       step{1}=a(1:inter(1));
    elseif i==length(inter)+1
           step{i}=a(inter(i-1):end);
    else
        step{i}=a(inter(i-1)+1:inter(i));
    end
end

问题10、怎么自定义sum函数求和

 俺斜率以类似与matlab自带的sum函数起名为NewSum

clc
clear
x=round(10*rand(20,10));%随机生成1-10之间的20行10列的整数矩阵
sum_col=NewSum(x,1);
sum_row=NewSum(x,2);


function Y=NewSum(x,flag)
if flag==1
    fg1=2;
    fg2=1;
elseif   flag==2
    fg1=1;
    fg2=2;
else
    error('参数不存在')
end
n=0;
for i=1:size(x,fg1)    %列
    n=n+1;
    sumx=0;
    for j=1:size(x,fg2)%行
        if flag==1
            sumx=x(j,i)+sumx;
        else
            sumx=x(i,j)+sumx;
        end
    end
    Y(i)=sumx;
end
end

问题11、怎么找出两个矩阵中相同列所在的位置索引呢

clc
clear
A=[1,5,7,9,11;
    3,4,7,9,10;
    6,5,1,8,9];
B=[1,7,11;
    3,7,10;
    6,1,9];
for i=1:size(A,2)
    for j=1:size(B,2)
        flag=(A(:,i)==B(:,j));
        if flag==1
            disp(['idx:',num2str(i)])
        end
    end
end

 运行结果:

 

问题12、

X = randn(10000,1);
h = histogram(X,40,'FaceColor',[0,0,1],'EdgeColor','none');
hold on
x=h.BinEdges(2:end);
y=h.Values;
ax=get(gca);
line([-2,-2],[0,450],'Color','k','LineStyle','-','linewidth',1)
line([2,2],[0,450],'Color','k','LineStyle','-','linewidth',1)

annotation('textarrow',[0.34 0.46], [0.5 0.5],'color','r');
annotation('textarrow',[1-0.34 0.54], [0.5 0.5],'color','r');
text(0,400,'94%','color','r','fontname','Times new roman','fontsize',16)

a1 = 795.5;
b1 =  0.08711;
c1 = 1.414;

f= a1*exp(-((x-b1)/c1).^2);
plot(x,f,'-.r','linewidth',1)

 

问题12 数据拟合问题

ncoeffs = numcoeffs(f);
coeffs = coeffnames(f);
f = fittype('Fourier1');
coeffnames(f);
formula(f);
[c,gof] = fit(x',y',f);
coeffvalues(c)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好玩的Matlab(NCEPU)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值