octave下实现积分/求解微分方程

我电脑是AMD的不是Intel的,所以matlab很多功能用不了(矩阵乘法都用不了),虚拟机也没工夫折腾,找了octave来替代。

 

首先安装 symbolic包(要求已安装好Python3的SymPy库)

pkg install -forge symbolic

使用pkg list可以看到symbolic已经安装上了

然后导入这个package

pkg load symbolic

定义一个sym试试

>> sym x
Symbolic pkg v2.7.1: Python communication link active, SymPy v1.1.1.
ans = (sym) x

定义一个函数

>> function y = f (x)
y = exp(x);
endfunction

对函数f从负无穷到0做定积分

>> [v, ier, nfun, err] = quad("f",-inf,0)
v =  1.0000
ier = 0
nfun =  135
err =  0.000000000058426

官方文档大意:

积分结果返回给q,如果ier是0表示积分成功,nfun是创建的function evaluations的数量,err是误差

q = quad (f, a, b)

[q, ier, nfun, err] = quad (…)

 

a and b are the lower and upper limits of integration. Either or both may be infinite.

The result of the integration is returned in q.

ier contains an integer error code (0 indicates a successful integration).

nfun indicates the number of function evaluations that were made.

err contains an estimate of the error in the solution.

如果是不定积分,和matlab一样用的是int函数求,就是有点慢,而且输出的符号有点好笑(?)不过octave安装很方便可以原谅

>> syms x
>> y = 1/(1+cos(x))^2;
>> int(y)
ans = (sym)

     3/x\      /x\
  tan |-|   tan|-|
      \2/      \2/
  ------- + ------
     6        2

>>

同样的,使用int函数也可以求定积分,也是慢一点,quad函数求出来的比较快

使用vpa(s,5)将定积分的结果取为小数点后五位的具体数值

>> s=int(y,1,2)
s = (sym)

                  3           3
    tan(1/2)   tan (1/2)   tan (1)   tan(1)
  - -------- - --------- + ------- + ------
       2           6          6        2

>> vpa(s,5)
ans = (sym) 1.1080
>>

 

参考这篇文章,使用octave还可以matlab兼容地求解微分方程

下面给出一个有条件的求解微分方程的例子

pkg load symbolic;
a=1;b=0.1;
syms k(t);
syms v(t);

F1 = (diff(k,t) + b*k == 0);
cond1 = k(0) == 2;
r1=dsolve(F1,cond1)

输出结果

warning: passing floating-point values to sym is dangerous, see "help sym"
warning: called from
    double_to_sym_heuristic at line 50 column 7
    sym at line 379 column 13
    mtimes at line 65 column 5
    mtimes at line 61 column 5
    my_week14 at line 9 column 4
r1 = (sym)

            -t
            ---
             10
  k(t) = 2*e

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值