Why I can't integrate a matrix in Matlab?

So for a while it worked, but than I ran the program again, and somehow it doesn't work again, though I did not change the code. The error message: "Attempt to execute SCRIPT integral as a function"
The code:
t=3000
a=1.044
b=-3.484*10^(-5)
k=0.0711
n=0.461
for i=1:t
MR(i)=a*exp(-k*(i^(n)))+b*i;
end
fun=@(i) MR;
MRint=integral(fun, 0, 2, 'Arrayvalued', 1)

5 Comments

t=3000;
a=1.044;
b=-3.484*10^(-5);
k=0.0711;
n=0.461;
fun= @(i)a*exp(-k*(i^(n)))+b*i;
MRint=integral(fun, 0, 2, 'Arrayvalued', 1);
Thank you, but it still doesn't work, I suspect that in this code, there's no "for cycle" for the "i".
Btw I am not very good at Matlab, so I would appreciate further help :)
Hey, Jani
Firstly you need tell what your script want to achieve, e.g., you integrate a function called 'fun', so which variable is the argument to this inline function, 'i' or other variables?
Hey Wan Ji
Well I am working on fluid-bed drying on my collegue thesis, and 'i' sometimes stands for 't' (time) or 'z' (height).
For example I calculate MR (Moisture Ratio) as a function of height, and I get a 1x3000 matrix, MR(z). Than I want to integrate this as a function of z.
Look at Walter Roberson's answer. Good solution to your problem

Sign in to comment.

 Accepted Answer

You saved your code into a file named integral.m which is interfering with calling MATLAB's integral() function.
for i=1:t
MR(i)=a*exp(-k*(i^(n)))+b*i;
end
After that, MR will be a vector of constants.
fun=@(i) MR;
That tells MATLAB that fun is an anonmous function that takes a single argument, and no matter what single argument is passed in, it should ignore the argument and return the content of the vector MR. It does not tell MATLAB that MR should be interpreted as a formula in i and integrated based on that formula. When you did the for i=1:t then i was given specific numeric values, 1, 2, 3, and so on, in turn, and that specific numeric value was used to calculate entries for MR.
You could integrate the vector of numeric values with respect to t, but the result is just going to be the numeric values times the difference in times.

5 Comments

Thanks for you answer!
And is there any way of making it work? If it's okay, I would copy a specific equation, that I need to calculate as a function of 'z' (height), and than I need to make a spatial avarage of that, by integrating it.
So as my understadning goes kszi_b(z) will become an [1 z] matrix, and I need to integrate that, to get the avarage of kszi_b.
Please ignore the constants, the essence here is the function 'z'.
You can give me a simplified code, that whit I can calculate this?
syms chi_e chi_0 K_be delta_b nu_b z chi_b(z)
syms H_f real
chi_b(z) = chi_e - (chi_e - chi_0) * exp(-z*K_be * delta_b / nu_b)
chi_b(z) = 
chi_b__tilde = int(chi_b, z, 0, H_f) / H_f
chi_b__tilde = 
collect(chi_b__tilde, [nu_b, K_be])
ans = 
Ohh thank you very much, it works now!
Only question remained to me is that as I can see, 'z' isn't included after the integration, at chi_b_tilde. Does that mean the equation doesn't change by changing the 'z' (height)?
I say so, because if I put the constants, the script works fine, but if I add the 'z' (e.q.: 2000), it gives me an error: "Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression."
chi_b__tilde involves the definite integral over z of a function of z. The (successful) result of a definite integral never involves the variable of integration.
So as my understadning goes kszi_b(z) will become an [1 z] matrix
No . Your chi_b(z) is "a specific equation, that I need to calculate as a function of 'z' (height)", and equations are not matrices. Consider for a moment if your heights are continuous. Suppose you have reached a height of 1000 at one point, and (somehow) you expect that would give a 1 x 1000 array. And a brief time later you have reached a height of 1001 and (somehow) you expect that would give a 1 x 1001 array. But because the heights are continuous, the height would have to pass through 1000+1/2 -- but it is not possible to have an array that is 1000+1/2 locations.
Sometimes people have a set of values of a function evaluated at known locations, and they want to find the approximate integral of the function over that range of locations. And in an expression such as
chi_b(z) = chi_e - (chi_e - chi_0) * exp(-z*K_be * delta_b / nu_b)
sometimes people get confused between formulas and array indexing, treating the z on the right hand side as a value, but treating the z on the left hand side as an array index. So on the right hand side they might be thinking they are working with the "third" z value, and they might be thinking that on the left hand side, matlab should know that chi_b(z) should be writing into the "third" output location. But that isn't how it works. The "third" z value might be (for example) 0.12 and it is the 0.12 that would have to be on the right hand side, but to assign to the third output location, the left hand side would have to be talking about chi_b(3) not chi_b(0.12)
If you have a vector of z values, then you can use
chi_b = chi_e - (chi_e - chi_0) * exp(-z*K_be * delta_b / nu_b);
approximate_integral = trapz(z, chi_b);
or if you need to be inside a loop, then
zvals = vector of values for z
numz = numel(zvals);
chi_b = zeros(size(zvals));
for zidx = 1 : numz
z = zvals(zidx);
chi_b(zidx) = chi_e - (chi_e - chi_0) * exp(-z*K_be * delta_b / nu_b);
end
approximate_integral = trapz(zvals, chi_b);
Notice that the right hand side uses the current value of z, but the left hand side uses the index of the value.
Okay, Thank you for the explanation!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 22 Aug 2021

Commented:

on 24 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!