简体   繁体   English

Matlab 中的迭代

[英]Iteration in Matlab

I want to solve some iteration in chemical engineering problem using matlab.我想使用 matlab 解决化学工程问题中的一些迭代。 So the problem that I want to solve is Calculating the inter convertion of series adiabatic reactors, here is the algorithm given:所以我要解决的问题是计算串联绝热反应器的相互转换,这里是给出的算法:

  1. Algorthm in reactor 1反应堆 1 中的算法

Algorithm in reactor 1反应堆 1 中的算法

  1. Algotirhm in reactor 2反应器 2 中的算法

Algorithm in reactor 2反应堆 2 中的算法

I have been solved it in VBA Excel and the code is:我已经在 VBA Excel 中解决了它,代码是:

Public Function intrap(h, y)
n = y.Count
If n Mod 2 > 0 Then
    MsgBox ("JUMLAH DATA HARUS GENAP")
    intrap = "ERROR:JUMLAH DATA HARUS GENAP"
Exit Function
End If

For i = 1 To n
s1 = s1 + y(i)
Next i

For i = 2 To (n - 1)
s2 = s2 + y(i)
Next i

intrap = (h / 2) * (y(1) + (2 * s2) + y(n))
End Function

and the output is Output和 output 是Output

So, how to do it in matlab?那么,如何在 matlab 中做到这一点?

It should be something like this:它应该是这样的:

function result = intrap(h, y)
    n = length(y);
    if n % 2 > 0
        disp("JUMLAH DATA HARUS GENAP")
        result = 'ERROR:JUMLAH DATA HARUS GENAP';
        return
    end

    for i = 1:n
        s1 = s1 + y(i);
    end

    for i = 2:(n - 1)
        s2 = s2 + y(i);
    end

    result = (h / 2) * (y(1) + (2 * s2) + y(n));
end

Id recommend you doing a few tutorials.我建议你做一些教程。 You'll see that Matlab is pretty straight forward for easy programming.您会看到 Matlab 非常简单,易于编程。

Sheers薄纱

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

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