# Why I can't integrate a matrix in Matlab?

조회 수: 1(최근 30일)
Jani 2021년 8월 22일
댓글: Jani 2021년 8월 24일
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표시숨기기 이전 댓글 수: 4
Wan Ji 2021년 8월 23일
Look at Walter Roberson's answer. Good solution to your problem

댓글을 달려면 로그인하십시오.

### 채택된 답변

Walter Roberson 2021년 8월 22일
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표시숨기기 이전 댓글 수: 4
Jani 2021년 8월 24일
Okay, Thank you for the explanation!

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!