Why I can't integrate a matrix in Matlab?
1 view (last 30 days)
Show older comments
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
Accepted Answer
Walter Roberson
on 22 Aug 2021
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
Walter Roberson
on 23 Aug 2021
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!