简体   繁体   English

需要帮助在Matlab中绘制此函数

[英]Need help plotting this function in Matlab

I recently started using Matlab and I am trying to plot Imaginary part of a function. 我最近开始使用Matlab,我试图绘制Imaginary函数的一部分。 I can see I am making a mistake somewhere because I have the graph showing what I need to get and I am getting something else. 我可以看到我在某个地方犯了一个错误,因为我有图表显示我需要得到的东西,而我正在得到别的东西。 Here is the link of the graph that I need to get, that I am getting and a function that I am plotting: 这是我需要得到的图表的链接,我得到的和我正在绘制的函数:

想要的结果,绘制函数和获得的图形。

I know that this function has a singularity at frequency around 270 Hz and I don't know if a 'quadgk' can solve integral then. 我知道这个函数在270 Hz左右的频率上有一个奇点,我不知道'quadgk'是否能解决积分。 You guys probably know but theta function inside the integral is a heaviside and I don't know if that is causing problem maybe? 你们可能知道但是积分里面的θ功能是一个重量级,我不知道这是否会导致问题? Is there something wrong I am doing here? 我在这里做错了吗? Here is the matlab code I wrote: 这是我写的matlab代码:

clear all
clc

ff=1:10:1000;

K1=(2*3)*log(2*cosh(135/6))/pi;
theta1=ones(size(ff));
theta1(ff<270)=0;

I1=zeros(size(ff));

for n = 1:numel(ff)
    f = ff(n);
    I1(n)= K1/f + (f/pi)*quadgk(@(x)(sinh(x/3)/(cosh(135/3)+cosh(x/3))-theta1(n))./((f^2)-4*(x.^2)), 0, inf);
end

plot(ff,I1, 'b');
hold on
axis([0 1000 -0.3 1])
  • First, I altered the expression for your heaviside function to what I think is the correct form. 首先,我将你的重力函数的表达式改为我认为正确的形式。
  • Second, I introduced the definitions of mu and T explicitly. 其次,我明确地介绍了mu和T的定义。
  • Third, I broke down the integral into 4 components, as follows, and evaluated them individually (along the lines of what Fraukje proposed) 第三,我将积分分解成4个组成部分,如下所示,并单独评估它们(沿着Fraukje提出的方向)
  • Fourth, I use quadl , although this is prob. 第四,我使用quadl ,虽然这是概率。 of minor importance. 不重要的。
  • ( edit ) Changed the range of ff 编辑 )改变了ff的范围

Here's the code with changes: 这是带有更改的代码:

fstep=1;
ff=[1:fstep:265 275:fstep:1000];

T = 3;
mu = 135;

df = 0.01;
xmax = 10;

K1=(2*T/pi)*log(2*cosh(mu/(2*T)));
theta1=ones(size(ff));
theta1(ff-2*mu<0)=0;

I1=zeros(size(ff));    
for n = 1:numel(ff)
    f = ff(n);
    sigm1 = @(x)  sinh(x/T)./((f^2-4*x.^2).*(cosh(mu/T)+cosh(x/T)));
    sigm2 = @(x)   -theta1(n)./(f^2-4*x.^2);
    I1(n) = K1/f  + (f/pi)*quadl(sigm1,0,f/2-df);      % term #1
%    I1(n) = I1(n) + (f/pi)*quadl(sigm1,f/2+df,xmax);   % term #2
%    I1(n) = I1(n) + (f/pi)*quadl(sigm2,0,f/2-df);     % term #3
%    I1(n) = I1(n) + (f/pi)*quadl(sigm2,f/2+df,xmax);   % term #4
end

I selected to split the integrals around x=f/2 since there is clearly a singularity there (division by 0). 我选择将积分分成x=f/2因为那里有明显的奇点(除以0)。 But additional problems occur for terms #2 and #4, that is when the integrals are evaluated for x>f/2 , presumably due to all of the trigonometric terms. 但是对于术语#2和#4会出现其他问题,即当积分被评估为x>f/2 ,可能是由于所有三角项。

If you keep only terms 1 and 3 you get something reasonably similar to the plot you show: 如果你只保留条款1和3你得到相当类似向您展示的情节的东西:

在此输入图像描述

However you should probably inspect your function more closely and see what can be done to evaluate the integrals for x>f/2 . 但是,你应该更仔细地检查你的函数,看看可以做些什么来评估x>f/2的积分。

EDIT 编辑

I inspected the code again and redefined the auxiliary integrals: 我再次检查了代码并重新定义了辅助积分:

I1=zeros(size(ff));
I2=zeros(size(ff));
I3=zeros(size(ff));

for n = 1:numel(ff)
    f = ff(n);
    sigm3 = @(x)  sinh(x/T)./((f^2-4*x.^2).*(cosh(mu/T)+cosh(x/T))) -theta1(n)./(f^2-4*x.^2);
    I1(n) = K1/f  + (f/pi)*quadl(sigm3,0,f/2-df); 
    I2(n) = (f/pi)*quadl(sigm3,f/2+df,10);
end
I3=I2;
I3(isnan(I3)) = 0;
I3 = I3 + I1;

This is how the output looks like now: 这就是输出现在的样子:

在此输入图像描述

The green line is the integral of the function over 0<x<f/2 and seems ok. 绿线是0<x<f/2以上函数的积分,看起来还可以。 The red line is the integral over Inf>x>f/2 and clearly fails around f=270 . 红线是Inf>x>f/2的积分,并且在f=270附近明显失败。 The blue curve is the sum (the total integral) excluding the NaN contribution when integrating over Inf>x>f/2 . 蓝色曲线是在Inf>x>f/2积分时不包括NaN贡献的总和(总积分)。

My conclusion is that there might be something wrong with the curve as you expect it to look. 我的结论是,曲线可能会出现一些问题,正如您所期望的那样。

So far I'd proceed this way: 到目前为止,我会这样做:

clc,clear

T = 3;
mu = 135;

f = 1E-04:.1:1000;

theta = ones(size(f));
theta(f < 270)= 0;

integrative = zeros(size(f));
for ii = 1:numel(f)
    ff = @(x) int_y(x, f(ii), theta(ii));
    integrative(ii) = quad(ff,0,2000);
end

Imm = ((2*T)./(pi*f)).*log(2*cosh(mu/(2*T))) + (f/pi).*integrative;

Imm1 = exp(interp1(log(f(1:2399)),log(Imm(1:2399)),log(f(2400):.001:f(2700)),'linear','extrap'));
Imm2 = exp(interp1(log(f(2985:end)),log(Imm(2985:end)),log(f(2701):.001:f(2984)),'linear','extrap'));


plot([(f(2400):.001:f(2700)) (f(2701):.001:f(2984))],[Imm1 Imm2])
hold on
axis([0 1000 -1.0 1])
plot(f,Imm,'g')
grid on
hold off

with

function rrr = int_y(x,f,theta)
T = 3;
mu = 135;
rrr = ( (sinh(x./T)./(cosh(mu/T) + cosh(x/T))) - theta ) ./ (f.^2 - 4.*(x.^2));
end

I've come up with this plot: 我想出了这个情节:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM