Why I can't integrate a matrix in Matlab?

1 ビュー (過去 30 日間)
Jani
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 件のコメント
Wan Ji
Wan Ji 2021 年 8 月 23 日
Look at Walter Roberson's answer. Good solution to your problem

サインインしてコメントする。

採用された回答

Walter Roberson
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 件のコメント
Jani
Jani 2021 年 8 月 24 日
Okay, Thank you for the explanation!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by