Asked by Marina Gonzalez
on 23 May 2019

I have to iterate the conversion of a reaction (x) in order to get the different values of catalyst mass (m).

The conversion 'x' would go from 0 to 1 with spaces of 0.05.

The function I need to iterate is: m=F*(k_kin*C*(1-x))/(1+k_ads*C*(1-x))

I know the value of all the variables except for m, which is the mass that I'd like to calculate.

F=0.11574; k_kin=1.3*10^-6; C=1.086; k_ads=0.9986

Please, help me. I haven't used matlab in years and I've forgotten how to make a 'for' loop work.

Thank you in advance.

Answer by Star Strider
on 23 May 2019

Accepted Answer

Everything except ‘x’ are scalars, so you can do this using a vectorization approach without the loop:

F=0.11574; k_kin=1.3*10^-6; C=1.086; k_ads=0.9986;

x = 0:0.05:1;

m=F*(k_kin*C*(1-x))./(1+k_ads*C*(1-x));

figure

plot(x, m)

grid

xlabel('Reaction')

ylabel('Catalyst Mass')

... including the plot! (Note the (./) denoting element-wise division.)

Marina Gonzalez
on 23 May 2019

Thank you! I have the same problem I had before. I'm doing something wrong because the plot is not right, but at least now I know how to write it down.

Thank you again!

Star Strider
on 23 May 2019

As always, my pleasure!

Sign in to comment.

Answer by Geoff Hayes
on 23 May 2019

Edited by Geoff Hayes
on 23 May 2019

Marina - try

m = zeros(20,1);

k = 1;

for x = 0:0.05:1

m(k) = F*(k_kin*C*(1-x))/(1+k_ads*C*(1-x))

k = k + 1;

end

edit to above: repalce == with = in assignment to m

madhan ravi
on 23 May 2019

plot(x, F*(k_kin*C*(1-x))./(1+k_ads*C*(1-x)))

Geoff Hayes
on 23 May 2019

where is x defined? I think you are missing

x = 0:0.05:1;

just prior to where you define m.

Marina Gonzalez
on 23 May 2019

Thank you! That has really helped. I think I'm doing something wrong because for a conversion of 0, the mass of the catalyst is way bigger than for the total conversion.

I'm doing something wrong but I don't know what. I'll think about it.

Thank you once again!

Sign in to comment.

Answer by Marina Gonzalez
on 23 May 2019

It's me again- sorry!

What if the function instead of being the one I said before was m(k) = F*(1+k_ads*C*(1-x))/(k_kin*C*(1-x))

Would anything change? Because when I plot this new equation nothing shows up on the graph

Star Strider
on 23 May 2019

It shows up when I plot it!

Try this:

F=0.11574; k_kin=1.3*10^-6; C=1.086; k_ads=0.9986;

x = 0:0.05:1;

m = F*(1+k_ads*C*(1-x))./(k_kin*C*(1-x));

figure

plot(x, m)

grid

xlabel('Reaction')

ylabel('Catalyst Mass')

Remember to use element-wise division (./).

Marina Gonzalez
on 23 May 2019

Thank you sooooo much! It works! You've saved my day! :)

Star Strider
on 23 May 2019

As always, my pleasure!

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## madhan ravi (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/463704-how-to-iterate-xa-from-0-to-1-with-steps-of-0-05-in-the-function-r-f-k-c-1-xa-1-k-c-1-xa-al#comment_708127

Sign in to comment.