0% found this document useful (0 votes)
89 views4 pages

Matlab Assignment 2 ACTUALL

This document contains Matlab code for several assignments involving numerical integration and solving nonlinear equations. It defines functions for hyperbolic trigonometric functions and plots their behavior. It also uses numerical integration techniques like the trapezoidal rule and Richardson extrapolation to calculate integrals of functions and estimates the errors at different step sizes. Graphs of the errors are also plotted.

Uploaded by

Zara Thomas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views4 pages

Matlab Assignment 2 ACTUALL

This document contains Matlab code for several assignments involving numerical integration and solving nonlinear equations. It defines functions for hyperbolic trigonometric functions and plots their behavior. It also uses numerical integration techniques like the trapezoidal rule and Richardson extrapolation to calculate integrals of functions and estimates the errors at different step sizes. Graphs of the errors are also plotted.

Uploaded by

Zara Thomas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

%% Matlab Assignment 2

% 2.
subplot(1,2,1), ezplot('x + y=1', [-2 2])
hold
subplot(1,2,1), ezplot('x^2 - y^2 = 1', [-2 2])
subplot(1,2,2), ezplot('x + y=1', [-10 10])
hold
subplot(1,2,2), ezplot('x^2 - y^2 = 9', [-10 10])

% 1a.
subplot(3,2,1), ezplot('cosh(x)', [-3 3]);
subplot(3,2,2), ezplot('sinh(x)',[-3 3]);
subplot(3,2,3), ezplot('tanh(x)',[-3 3]);
subplot(3,2,4), ezplot('sech(x)',[-3 3]);
subplot(3,2,5), ezplot('csch(x)',[-3 3]);
subplot(3,2,6), ezplot('coth(x)',[-3 3])

% 1b.
subplot(3,2,1), ezplot('cosh(x)', [-3 3]);
subplot(3,2,2), ezplot('asinh(x)',[-3 3]);
subplot(3,2,3), ezplot('atanh(x)',[-3 3]);
subplot(3,2,4), ezplot('asech(x)',[-3 3]);
subplot(3,2,5), ezplot('acsch(x)',[-3 3]);
subplot(3,2,6), ezplot('acoth(x)',[-3 3])

% 1c.
a=evalin(symengine,'rewrite(cosh(x),exp)');
b=evalin(symengine,'rewrite(sech(x),exp)');
c=evalin(symengine,'rewrite((cosh(x) + sinh(x)),exp)');
d=evalin(symengine,'rewrite(sech(x),exp)');
e=evalin(symengine,'rewrite(sinh(x),exp)');
f=evalin(symengine,'rewrite(tanh(x),exp)');
g=evalin(symengine,'rewrite((cosh(x) - sinh(x)),exp)');
h=evalin(symengine,'rewrite((cosh(x)^2 - sinh(x)^2),exp)')
simplify(a);
simplify(b);
simplify(c);
simplify(d);
simplify(e);
simplify(f);
simplify(g);
simplify(h)

% 1d.
y = sym('y'), x = sym('x');
simplify(2*cosh(x)*sinh(x));
simplify(cosh(x)^2 + sinh(x)^2);
simplify(1 - tanh(x)^2);
simplify((cosh(x)^2 - 1) / 2);
simplify((cosh(x)^2 + 1) / 2);
simplify(coth(x)^2 - 1);
simplify(cosh(x)*sinh(y) + sinh(x)*cosh(y));
simplify(cosh(x)*cosh(y) + sinh(x)*sinh(y))
% 3a.
fplot(@(x)[exp(x),2 - x^2],[-sqrt(2) sqrt(2)])
xplus = solve('x=+sqrt(2-exp(x))')
xminus = solve('x=-sqrt(2-exp(x))')

% 3b.

xit(1) = vpa(1);
for its = 1:6
xit(its+1) = xit(its) -(exp(xit(its)) + xit(its)^2 - 2)/(exp(xit(its)) + 2*xit(its));
end
xit';
i = [(0:6)'];
f = (@(x)[exp(x) + x.^2 - 2]);fit = f(xit');
format short e, fit, single(fit); W = ans;
er = (abs(xplus - xit'));format short e, er, single(er); X = ans;
Table = [i,xit',W,X]

% 3c.
xiu(1) = vpa(-sqrt(2));
for ius = 1:5
xiu(ius+1) = xiu(ius) -(exp(xiu(ius)) + xiu(ius)^2 - 2)/(exp(xiu(ius)) + 2*xiu(ius));
end
xiu';
ii = [(0:5)'];
f = (@(x)[exp(x) + x.^2 - 2]);fiu = f(xiu');
format short e, fiu, single(fiu); Y = ans;
err = (abs(xminus - xiu'));format short e, err, single(err); Z = ans;
Table = [ii,xiu',Y,Z]

% 3d.
xit(1) = vpa(0);
for its = 1
xit(its + 1) = vpa(1);
for its = 2:9
xit(its+1) = xit(its) - (((xit(its) - xit(its-1))/((exp(xit(its))+((xit(its))^2)-2)-(exp(xit(its-1))+((xit(its-1))^2)-
2)))*(exp(xit(its))+((xit(its))^2)-2));xit(its+1) = xit(its) - (((xit(its) - xit(its-1))/((exp(xit(its))+((xit(its))^2)-
2)-(exp(xit(its-1))+((xit(its-1))^2)-2)))*(exp(xit(its))+((xit(its))^2)-2));
end
xit'
end
i = [(0:9)'];
f = (@(x)[exp(x) + x.^2 - 2]);fit = f(xit');
format short e, fit, single(fit); W = ans;
er = (abs(xplus - xit'));format short e, er, single(er); X = ans;
Table = [i,xit',W,X]

% 3e.
xit(1) = vpa(0);
for its = 1
xit(its + 1) = vpa(-sqrt(2));
for its = 2:9
xit(its+1) = xit(its) - (((xit(its) - xit(its-1))/((exp(xit(its))+((xit(its))^2)-2)-(exp(xit(its-1))+((xit(its-1))^2)-
2)))*(exp(xit(its))+((xit(its))^2)-2));xit(its+1) = xit(its) - (((xit(its) - xit(its-1))/((exp(xit(its))+((xit(its))^2)-
2)-(exp(xit(its-1))+((xit(its-1))^2)-2)))*(exp(xit(its))+((xit(its))^2)-2));
end
xit'
end
i = [(0:9)'];
f = (@(x)[exp(x) + x.^2 - 2]);fit = f(xit');
format short e, fit, single(fit); W = ans;
er = (abs(xminus - xit'));format short e, er, single(er); X = ans;
Table = [i,xit',W,X]

% 4a.
x = sym('x');
n4a = @(x)exp(-x^2);
a = 0; b = 1;
int(n4a(x),a,b);
Exactintegral = double(ans)
for l = 1:6
N = 2^(l-1); h = (b-a)/N;
sum = 0;
for i = 1:N-1
xi = a + i*h;
sum = sum + n4a(xi);
end
K_0(l) = h*(0.5*n4a(a)+sum+0.5*n4a(b));
end
for k = 1:5
A = zeros(1,6-k);
B = eval(['K_' num2str(k-1)]);
for l = 1:6-k
A(l) = ((4^k)*B(l+1)-B(l))/((4^k)-1);
end
eval(['K_' num2str(k) '=A'])
end
for k = 0:5
A = abs((Exactintegral .*(ones(1,6-k)))-(eval(['K_' num2str(k)])));
eval(['Er_' num2str(k) '= A'])
end
loglog([1 2 4 8 16 32],Er_0,'r',[2 4 8 16 32],Er_1,'c',[4 8 16 32],Er_2,'b',[8 16 32],Er_3,'g',[16 32],Er_4,'y',
[32],Er_5,'m')
xlabel('N'); ylabel('error')
legend('Row 1','Row 2','Row 3','Row 4','Row 5','Row 6');

% 4b.
n4b = @(x)(sin(x)^2)/((1+cos(x)^2)^0.5);
a = 0; b = 2*pi;
int(n4b(x),a,b);
exactint4b = double(ans)
K2_0 = zeros(1,20)
for ii = 1:20
N = 2*ii; h = (b-a)/N;
sum = 0;
for jj = 1:N-1
xi = a + jj*h;
sum = sum + n4b(xi);
end
K2_0(j) = h*(0.5*n4b(a)+sum+0.5*n4b(b));
end
for jj = 1:5
M = zeros(1,20-jj);
L = eval(['K2_' num2str(jj-1)]);
for ii = 1:20-k
M(ii) = ((4^jj)*L(ii+1)-L(ii))/((4^ii)-1);
end
eval(['K2_' num2str(jj) '=M'])
end
for jj = 0:5
M = abs((exactint4b.*(ones(1,20-jj)))-(eval(['K2_' num2str(jj)])));
eval(['Er2_' num2str(jj) '= M'])
end
semilogy(2:2:40,Er2_0,'r',4:2:40,Er2_1,'c',6:2:40,Er2_2,'b',8:2:40,Er2_3,'y',10:2:40,Er2_4,'k',12:2:40,Er2_5
,'g')
xlabel('N'); ylabel('Error')
legend('Row 1','Row 2','Row 3','Row 4','Row 5','Row 6');

You might also like