Integral command doesn’t work inside the for loop
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(‘tAnalyticaltTrapezoidtSimpsonnn’)
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%gtttt%gttt%gn’,I(i),Trap(i),Simp(i))
end
f = @(x) exp(x);
Trap = 0.5*h1*(f(a) + f(b));
Simp = (h2/3)*(f(a) + 4*f(h2) + f(b));
fprintf(‘t%gttt%gtt%gn’,I,Trap,Simp)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(‘tAnalyticaltTrapezoidtSimpsonnn’)
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%gtttt%gttt%gn’,I(i),Trap(i),Simp(i))
end
f = @(x) exp(x);
Trap = 0.5*h1*(f(a) + f(b));
Simp = (h2/3)*(f(a) + 4*f(h2) + f(b));
fprintf(‘t%gttt%gtt%gn’,I,Trap,Simp) 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(‘tAnalyticaltTrapezoidtSimpsonnn’)
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%gtttt%gttt%gn’,I(i),Trap(i),Simp(i))
end
f = @(x) exp(x);
Trap = 0.5*h1*(f(a) + f(b));
Simp = (h2/3)*(f(a) + 4*f(h2) + f(b));
fprintf(‘t%gttt%gtt%gn’,I,Trap,Simp) numerical integration, trapezoid rule, simpson rule, integral command MATLAB Answers — New Questions