Integral command doesn't work inside the for loop

1 回表示 (過去 30 日間)
Left Terry
Left Terry 2025 年 1 月 6 日
コメント済み: Left Terry 2025 年 1 月 6 日
Hello. I have this simple code trying to compare analytical solution of few functions with two numerical methods. I used for loop wih the integral command in it but i get many errors.
clear, clc, close all, format long
a = 0;
b = 2;
h1 = b - a;
h2 = (b - a)/2;
n = 1:5;
fprintf('\tAnalytical\tTrapezoid\tSimpson\n\n')
Analytical Trapezoid Simpson
for i = 1:length(n)
f = @(x) x.^n;
I = integral(f,a,b);
Trap = 0.5*h1*(f(a) + f(b));
Simp = (h2/3)*(f(a) + 4*f(h2) + f(b));
fprintf('\t%g\t\t\t\t%g\t\t\t%g\n',I(i),Trap(i),Simp(i))
end
Error using .^
Arrays have incompatible sizes for this operation.

Error in solution>@(x)x.^n (line 12)
f = @(x) x.^n;

Error in integralCalc>iterateScalarValued (line 334)
fx = FUN(t);

Error in integralCalc>vadapt (line 148)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen, ...

Error in integralCalc (line 77)
[q,errbnd] = vadapt(vfunAB,interval, ...

Error in integral (line 87)
Q = integralCalc(fun,a,b,opstruct);
f = @(x) exp(x);
Trap = 0.5*h1*(f(a) + f(b));
Simp = (h2/3)*(f(a) + 4*f(h2) + f(b));
fprintf('\t%g\t\t\t%g\t\t%g\n',I,Trap,Simp)

採用された回答

Walter Roberson
Walter Roberson 2025 年 1 月 6 日
編集済み: Walter Roberson 2025 年 1 月 6 日
a = 0;
b = 2;
h1 = b - a;
h2 = (b - a)/2;
n = 1:5;
fprintf('\tAnalytical\tTrapezoid\tSimpson\n\n')
Analytical Trapezoid Simpson
for i = 1:length(n)
f = @(x) x.^n(i);
I(i) = integral(f,a,b);
Trap(i) = 0.5*h1*(f(a) + f(b));
Simp(i) = (h2/3)*(f(a) + 4*f(h2) + f(b));
fprintf('\t%g\t\t\t\t%g\t\t\t%g\n',I(i),Trap(i),Simp(i))
end
2 2 2 2.66667 4 2.66667 4 8 4 6.4 16 6.66667 10.6667 32 12
f = @(x) exp(x);
Trap = 0.5*h1*(f(a) + f(b));
Simp = (h2/3)*(f(a) + 4*f(h2) + f(b));
fprintf('\t%g\t\t\t%g\t\t%g\n',I,Trap,Simp)
2 2.66667 4 6.4 10.6667 8.38906 6.42073
However, the final printout is nonsense. You are printing out the vector I from the for i loop, along with the scalars Trap and Simp that you just calculated.
  1 件のコメント
Left Terry
Left Terry 2025 年 1 月 6 日
@Walter Roberson You are right about the final printout, i was focused on the for loop. Anyway it was in front of my eyes, thanks for your help. I think it's working correctly now
clear, clc, close all, format long
a = 0;
b = 2;
h1 = b - a;
h2 = (b - a)/2;
n = 1:5;
fprintf('\tAnalytical\tTrapezoid\tSimpson\n\n')
Analytical Trapezoid Simpson
for i = 1:length(n)
f = @(x) x.^(n(i));
I = integral(f,a,b);
Trap = 0.5*h1*(f(a) + f(b));
Simp = (h2/3)*(f(a) + 4*f(h2) + f(b));
fprintf('\t%.3f\t\t%.3f\t\t%.3f\n',I,Trap,Simp)
end
2.000 2.000 2.000 2.667 4.000 2.667 4.000 8.000 4.000 6.400 16.000 6.667 10.667 32.000 12.000
f = @(x) exp(x);
I = integral(f,a,b);
Trap = 0.5*h1*(f(a) + f(b));
Simp = (h2/3)*(f(a) + 4*f(h2) + f(b));
fprintf('\t%.3f\t\t%.3f\t\t%.3f\n',I,Trap,Simp)
6.389 8.389 6.421

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStability Analysis についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by