Problem 1 - Insert into a sorted array sorting
The program below works similarly to the program we covered in our last class. It takes a value and inserts it into the appropriate place in array A, which in this case is presorted from greatest to least. The rest of the array elements should remain unchanged Use the test value ``7''. Identify the problem and use the Matlab debugger to find and fix the problem.
A = [ 50 10 5 2 1 0]; %Initialize Array
x = input('Enter the New Value');
disp('Old Array is ');
disp(A);
current = 1 ; % Initialize array index
last = size(A,2); %Get number of elements in array
% Find the index where x belongs!
while(current<=last)
if x < A(current)
current = current + 1;
else
break;
end
end
% Move all the remaining elements up 1
if (current < last) % Need to move some up
for index = last:1:current
A(index+1) = A(index);
end
end
A(current) = x;
disp('New Array is');
disp(A);
Problem 2 - Numerical Integration
The program given below is supposed to implement Simpson's Composite Rule for numerical integration, which states that, given a function f(x), the integral of this function can be approximated by:
òxNx1f(x)dx @ [(D)/3][f(x1)+4f(x2)+2f(x3)+4f(x4)+2f(x5)¼
¼2f(xN-2)+4f(xN-1)+f(xN)
where D = [((xN-x1))/((N-1))] and xn = x1+(n-1)D and N is any odd integer (1,3,5,7,9,etc.).
In this case the program is integrating the function f(x) = x2+4 over the range 0 to 5 with N=9.
Unfortunately, the program is producing the wrong result. Find the bug anf fix the program so that it gives the correct answer for this integral.
File integrate.m:
N = 9; %Number of points in integration
x1 = 0; % Lower limit of integration
xN = 5; % Upper limit of integration
delta = (xN-x1)/(N-1);
integral = 0;
for n=1:1:N
xn = x1 + (n-1)*delta;
if n==1 | n==N
integral = integral + f(xn);
elseif mod(n,2)==0
integral = integral + 4*f(xn);
else
integral = integral + 2*f(xn);
end
integral = (delta/3)*integral;
end
fprintf('The integral of f.m between %g and %g is %g \n',x1,xN,integral);
File f.m:
function [y] = f(x)
y = x^2+4;
Section 2 - Searching and sorting programs.
For the problems below, use the following array for test data:
A = [1 3 7 5 6 8 2 4 3 7 1 0 9 7 5 8 11 13];
Problem 3 - Search and Count
Write a Matlab program to determine the frequency with which an element
appears in an array, e.g. in the example above, if the element in
question was ``7'' your program should report that ``7'' appears three
times in the test data. Accept the element to search on as a parameter
from the user.
Problem 4 - Array Sorting
Write a matlab program to implement a ``bubble sort'' to sort the test
array above from least to greatest. A bubble sort works according
to the following algorithm for an array with N elements:
M=N:
Compare the element to the next element
If they are the same, or the first one is smaller, do nothing
If the first one is larger, swap the two elements in the array.
(That gets the maximum into the last element in the array)
For all the problems above, turn in your M-file and your output. For
#1 and #2, highlight the error you corrected.
For elements 1 to M in the array:
Repeat above with M=M-1 or until no swaps are performed over the whole
array.