MATH 251 Exam Matlab, Spring 2015
American University of Beirut · Numerical Computing · 4 pages
More MATH 251 files
- Syllabus · Summer 2018LabView
- Syllabus · Summer 2018LabView
- Syllabus · Fall 2017LabView
- Exam Matlab · Summer 2015LabView
- Useful File Complete Book · Spring 2016FileView
- Exam 2 Multiple Previouses · Fall 2015ExamView
- Final Multiple Previouses · Fall 2015Final examView
- Exam 1 Previous Quiz 1 · Fall 2015ExamView
Show the text of this lab
MATLAB Quiz
Spring 2015
Version 2
Exercise1:
a. Given the arrays {(x(i),y(i))| i=1,2,…,n}, with x(i+1)-x(i)=h, for all I,write the MATLAB
function that computes:
.
function T= SumTrap(x,y)
% Inputs: x,y: 2 vectors of the same length
% Output: The above value T given by (1)
h=x(2)-x(1);
n=length(y);
T=y(1);
for i=2:n-1
T=T+2*y(i);
end
T=T+y(n);
T=T*0.5*h;
% Find T
end
Based on the arrays {(x(i),y(i)| i=1,2,…,n} with n=2k+1, and let h0=x(n)-x(1), consider the array
{T(h0),T(h0/2),…,T(h0/2k)} and consequently:
.
Now write the MATLAB function that implements the above formulae (2) and outputs the array
T and the lower triangular matrix R of size.
Call for the previous function if needed.
function [T,R]=Richard(x,y,k)
% Inputs: x,y: 2 vectors of length 2k+1
% k:integer.
% Output: T: a vector of length k+1
% R: a lower triangular matrix of size k×k as given in(2)
T=zeros(k+1,1);
R=zeros(k,k);
% You will need to generate a column vector h of length k+1 with
% h(1)=x(n)-x(1) and h(i)=h(i-1)/2
function [ T R] = richard( x,y,k )
n=length(y);
h=[];
T=[];
R=[];
h0=x(n)-x(1);
for i=1:k
h=[h h0/(power(2,i))]
end
for j=1:k
T=[T sumtrap(x,y)*(2/(x(2)-x(1)) *h(j))]
end
for l=1:k
for m=l:k
R=[R (4T(m)-T(2m))/3]
end
R=[R power(4,l)*R(l)-R(2l)]
end
%%the variable l goes from 1 to k because the first column is full , the
%%variable m goes from l to k because the first row of the second column is
%%empty the first two row of the third too...
end
Test the function Richard using the following inputs:
x=linspace(0,3,5), and y={y(1),…,y(5)} such that y(i)=sin ((x(i))2).
%copy the commands you used and the results T and R in this space
T=
R=
Exercise 2:
Let be the function defined by the following series:
, for all x
with:
Express as a function of by finding such that:
Use the above relation in writing the MATLAB function T(x,p) to approximate f(x) up to a
precision p, described below.
function [t,N,y]=T(x,p)
% Input: x a number
% p an integer >=2
% Output:N: least integer such that
% t: array
% y:approximation to f(x)
function [ t,N,y ] = ex2( x,p )
t=[x/2];
i=1;
while t(i)>0.5*power(10,1-p)
t=[t (formula)]
end
f=f+t(i)
End
Test the function T(x,p) using x=1 and p=3,7,10. The results should be expressed in double
precision.
P
T(x,p)
3
7
10