首页 > 编程知识 正文

matlab定积分画,如何用matlab求像积分

时间:2023-05-06 01:08:00 阅读:228687 作者:4772

想了想还是发篇文章吧这样子阅读量会比发资源多一点(bushi

首先参考了https://jingyan.baidu.com/article/bea41d437cbfb8b4c51be6bf.html的文章,但是这篇经验由于已经有些年头了,我发现其中比如mfun函数已经被matlab移除了,还是先按照这个思路来一遍:

使用matlab求极限:

>> syms t x positive % 定义t是正的gt = 1/log(t);gt_0 = limit(gt,t,0,'right') % 求 gt 在 t=0+ 处的极限 gt_0 = 0

利用图形观察在[0,1)区间的被积函数。(这一步也是出于谨慎考虑,有利于判断积分的可行性,便于粗略估计积分结果。

>> ezplot(gt,[0,1])title('1/log(t)')grid onlegend('gt')


关于被积函数的原函数的求解,如下:

>> fx = subs(int(gt,t),t,'x') fx = logint(x)

(注意这里出现的两个函数subs和int都很重要,待会儿会用到)

关于logint函数:(谁能想到它的定义就是对数积分呢

logint Integral logarithm.
Y = logint(X) is the integral logarithm of X.
It is defined as:
logint(x) = integral from 0 to x of 1/log(t) dt.
logint对数积分。 Y = logint(X)是对X的对数积分。 定义为: logint(x) =∫1/log(t) dt,从0到x。

对于积分:

int(S,v) is the indefinite integral of S with respect to v. v is a scalar SYM.
int(S,a,b) is the definite integral of S with respect to its symbolic variable from a to b. a and b are each double or symbolic scalars. The integration interval can also be specified using a row or a column vector with two elements, i.e., valid calls are also int(S,[a,b]) or int(S,[高贵的月饼]) and int(S,[a;b]).
int(S,v,a,b) is the definite integral of S with respect to v from a to b. The integration interval can also be specified using a row or a column vector with two elements, i.e., valid calls are also int(S,v,[a,b]) or int(S,v,[高贵的月饼]) and int(S,v,[a;b]).

int(S,v)是S对v的不定积分,v是标量SYM。
int(S,a,b)是S关于它的符号变量从a到b的定积分。a和b都是双重或符号标量。也可以使用包含两个元素的行或列向量来指定积分区间,也就是说,有效的调用也可以是int(S,[a,b])或int(S,[高贵的月饼])和int(S,[a;b])。
int(S,v,a,b)是S对v从a到b的定积分。积分区间也可以用包含两个元素的行或列向量指定,也可以是int(S,v,[a,b])或int(S,v,[高贵的月饼])和int(S,v,[a;b])。

这个时候我们已经通过上面那步知道gt的原函数(积分函数)是什么了,可以直接求:

>> x = [0:0.05:0.9];>> p = logint(x)p = 列 1 至 7 0 -0.0131 -0.0324 -0.0564 -0.0851 -0.1187 -0.1574 列 8 至 14 -0.2019 -0.2529 -0.3114 -0.3787 -0.4564 -0.5469 -0.6534 列 15 至 19 -0.7809 -0.9369 -1.1340 -1.3959 -1.7758

绘图验证一下是否正确:

hold onezplot(gt,[0,1]);plot(x,p,'--r');legend('gt','p','Location','Best')hold off

和开头经验中的绘制结果一样。

但是要把这个功能写成一整个函数的话,这样子中间插一步显示原函数,还得再自己写入原函数啥啥啥的肯定是不行的,用户体验感太差了(这么个小小的程序扯啥用户体验感呢?就是你自己嫌麻烦罢了)

改进之后的代码:

syms t;gt = 1/log(t);fx = subs(int(gt,t),t,'x');x = [0:0.05:0.9];p=subs(fx,x);hold onezplot(gt,[0,1]);plot(x,p,'--r');legend('gt','p','Location','Best')hold off

使用到我自己需要绘制的指数函数的积分上来说,只需要把上面这个代码稍微改一下就行:

syms t;gt = 0.1^t;fx = subs(int(gt,t),t,'x');x = [-4:0.2:4];p=subs(fx,x);hold onezplot(gt,[-4,4]);plot(x,p,'--r');legend('负指数函数','积分','Location','Best')hold off

绘制之后发现积分是负值,这就有些问题了,我要求的是指数函数在某个区域内的面积积分,指数函数又没有负值区域,怎么会面积是负数呢?
经过问题排查之后我确定我的具体代码没有问题,原来是算法有大差错——没有指定积分的下限,人家直接积出原函数之后就给赋值了,正常是需要指定积分下限之后上限赋值减下限的来着。

我这里就直接指定0为下限了:

syms t;gt = 0.1^t;fx = subs(int(gt,t),t,'x');x = [-4:0.2:4];p=subs(fx,x)-subs(fx,0);hold onezplot(gt,[-4,4]);plot(x,p,'--r');legend('负指数函数','积分','Location','Best')hold off

可以看到,结果图中积分最后是趋于一个极限的,非常合理的结果啊铁铁们(hhhhhh今天一天学疯了不要理会我的胡言乱语)。

整个函数稍微改一下就能套到其他的东西上,内容还算是相对完整吧。

(就这么点东西我研究了一个下午我可真是个代码菜鸡((哦当然下午也不只是只写了这个函数

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。