1

I want to solve some iteration in chemical engineering problem using 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

Algorithm in reactor 1

  1. Algotirhm in reactor 2

Algorithm in reactor 2

I have been solved it in VBA Excel and the code is:

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

So, how to do it in matlab?

Community
  • 1
  • 1

1 Answers1

0

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.

Sheers

Pablo Jeken Rico
  • 569
  • 5
  • 22