Problem in executing the code

1 Ansicht (letzte 30 Tage)
Aman Gupta
Aman Gupta am 4 Jun. 2021
Bearbeitet: per isakson am 4 Jun. 2021
%%
clc;
clear;
%Q.) 7
%Evaluate integral of exp(-x)/sqrt(x) over [0,1].
%(a) Using a rectangular rule
%(b) Make a change of variables x = t^2 and use rectangular rule on new variable.
%After applying change of variables we get the integral to find out equal
%to integral of exp(-x^2) over [-1,1].
syms f(x);
f(x) = exp(-x^2);
syms g(t);
g(t) = exp(-t)/t^(1/2);
a = -1;
b = 1;
N = [5 10 20 50 100 200 500 1000];
exact_int = 1.4936482656248540507989; % exact value of integral evauated from wolfram alpha.
Ngrids = 8; % Number of different grids
h = zeros(Ngrids,1);
rect_1 = zeros(Ngrids,1);
rect_2 = zeros(Ngrids,1);
for i = 1 : Ngrids
h(i)=(b-a)/N(i); % Different-different grid spacing
x = linspace(a,b,N(i)+1); % Grid Points
t = linspace(a,b,N(i)+1); % Grid Points
y1 = 0.5*(x(1:N(i))+x(2:N(i)+1));% Midpoints of each panel
y2 = 0.5*(t(1:N(i))+t(2:N(i)+1));% Midpoints of each panel
rect_1(i)=h(i)*sum(f(y1)); % Rectangular rule
rect_2(i)=h(i)*sum(g(y2)); % Rectangular rule after applying change of variables
end
error_rect_1 = abs(double(rect_1 - exact_int)); % absolute error using Rectangular rule
error_rect_2 = abs(double(rect_2 - exact_int)); % absolute error using Rectangular rule after applying change of variables
figure;
loglog(h,error_rect_1,'r','linewidth',1);
hold on
loglog(h,error_rect_2,'b','linewidth',1);
xlabel('Gridspacing');
ylabel('Error');
legend('Recangular rule' ,'Rectangular rule after applying change of variables','location','southeast','FontSize',7,...
'FontWeight','bold','Color','y');
  2 Kommentare
Aman Gupta
Aman Gupta am 4 Jun. 2021
Bearbeitet: per isakson am 4 Jun. 2021
%%
clc;
clear;
%Q.) 7
%Evaluate integral of exp(-x)/sqrt(x) over [0,1].
%(a) Using a rectangular rule
%(b) Make a change of variables x = t^2 and use rectangular rule on new variable.
%After applying change of variables we get the integral to find out equal
%to integral of exp(-x^2) over [-1,1].
syms f(x);
f(x) = exp(-x^2);
syms g(t);
g(t) = exp(-t)/t^(1/2);
a = -1;
b = 1;
c = 0;
N = [5 10 20 50 100 200 500 1000];
exact_int = 1.4936482656248540507989; % exact value of integral evauated from wolfram alpha.
Ngrids = 8; % Number of different grids
h1 = zeros(Ngrids,1);
h2 = zeros(Ngrids,1);
rect_1 = zeros(Ngrids,1);
rect_2 = zeros(Ngrids,1);
for i = 1 : Ngrids
h1(i)=(b-a)/N(i); % Different-different grid spacing
h2(i)=(b-c)/N(i);
x = linspace(a,b,N(i)+1); % Grid Points
t = linspace(c,b,N(i)+1); % Grid Points
y1 = 0.5*(x(1:N(i))+x(2:N(i)+1)); % Midpoints of each panel in x(i)'s
y2 = 0.5*(t(1:N(i))+t(2:N(i)+1)); % Midpoints of each panel in t(i)'s
rect_1(i)=h1(i)*sum(f(y1)); % Rectangular rule after applying change of variables
rect_2(i)=h2(i)*sum(g(y2)); % Rectangular rule
end
error_rect_1 = abs(double(rect_1 - exact_int)); % absolute error using Rectangular rule after applying change of variables
error_rect_2 = abs(double(rect_2 - exact_int)); % absolute error using Rectangular rule
figure;
loglog(h1,error_rect_1,'r','linewidth',1);
hold on
loglog(h2,error_rect_2,'b','linewidth',1);
hold on
loglog(h1,h1.^2,'k');
hold on
loglog(h2,h2.^2,'g');
xlabel('Gridspacing');
ylabel('Error');
title('"Error vs GridSpacing" for Rectangular Rules with and Without changing of variables','FontSize',14,...
'FontWeight','bold','Color','k');
legend('Recangular rule after applying change of variables' ,'Rectangular rule','h1^2','h2^2','location','southeast','FontSize',11,...
'FontWeight','bold','Color','y');
Aman Gupta
Aman Gupta am 4 Jun. 2021
The above code has run successfully, got the fault actually.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by