NEED answer ASAP!! and Please use a lot of comments!!! The purpose of the proble
ID: 3560178 • Letter: N
Question
NEED answer ASAP!! and Please use a lot of comments!!!
The purpose of the problem is to verify the binomial formula given by (n k) = 2n. Write a Mat lab program that asks the user to enter a positive integer N (not more than 20) from the keyboard. Then compute the quantity S1 = 2N and store it in the variable S1. Also compute the binomial sum S2 = (N k) and store it in the variable S2. Note that you may use the Matlab function nchoosek to compute the binomial coefficient (N k) = N!/k!(N - k)!. Your program should display the values of N, S1 and S2. Now compare S1 and S2, if they are equal, print "good news, they are equal"; otherwise, print "bad news, they are not equal".Explanation / Answer
%Clearing all variables from memory and then clearing the terminal
clear all
clc
%Value of N is inputted here
N = input('Enter a positive integer less than 20:');
%The variable integerTest stores a value 1 if N is an integer, else it is
%zero.
integerTest=~mod(N,1);
%Test if N > 20 or N < 1 or N is not an integer
if( (N>20) || (N < 1) || (integerTest == 0) )
disp('Incorrect Value of N, Restart From Beginning');
else
%If N > 20 and is an integer, evaluate the following
%Variable S1 stores the value 2^N.
S1 = 2^N;
%Initialize variable S2.
S2 = 0.0;
%Run a loop from k = 0 to N to sum up the combinatorical value
for(k=0:N)
S2 = S2 + nchoosek(N,k);
end
%If S1 and S2 are matching evaluate the following loop
if(S1 == S2)
disp('Good News, they are equal');
dispval = ['N = ',num2str(N),' S1 = ',num2str(S1),' S2 = ',num2str(S2)];
disp(dispval);
else
%If S1 and S2 do not match, evaluate the following loop
disp('Bad News, they are not equal');
dispval = ['N = ',num2str(N),' S1 = ',num2str(S1),' S2 = ',num2str(S2)];
disp(dispval);
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.