USC EE150 HW3/4/5

Published on March 2017 | Categories: Documents | Downloads: 88 | Comments: 0 | Views: 509
of 2
Download PDF   Embed   Report

Comments

Content

HW3:
a) P=2/3
b) P=1/3
c) P=1
d)
function [ x y ] = P(n,k,p,q)
a=factorial(n);
b=factorial(k);
c=factorial(n-k);
d = a / (b*c);
h = p^k;
m = q^(n-k);
x = d*h*m;
y=n;
disp([x y]);
end
HW4:
a) Boltz:
function y=Boltz(v)
m=1.67*32e-27;
T=300;
k=1.38e-23;
y = (((m)/(2*pi*k*T))^1.5)*4*pi*(v.^2).*exp(-(.5*m*v.^2)/(k*T));
end
b) Trap:
function y = trap(fn,a,b,h)
n = (b-a)/h;
x = a + [1:n-1]*h;
y = sum(feval(fn,x));
y = (h/2)*(feval(fn,a)+feval(fn,b)+2*y);
end
c) Fraction of molecules between 0 and 700 m/s:
trap(@Boltz,0,700,1): 0.9032
Fraction of molecules between 800 and 900 m/s:
trap(@Boltz,800,900,1): 0.0258
Fraction of molecules above 852 m/s:
1-trap(@Boltz,0,852,1): 0.0248

HW5:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(void)
{
int max=100; //max number for guessing
std::srand(std::time(0));
int target=rand()%(max+1);
bout << "Guess a number between 0 and " << max << " : > ";
int guess;
cin >> guess;
while(guess| = target)
{
cout<<guess<< " < target is " << (guess<target)
<< endl;
cout<<guess<< " > target is " << (guess>target)
<<endl;
cout<< "Guess a number between 0 and " << max << " :> ";
cin>>guess;
}
cout<< "Congratulations."
<< "You've just wasted a few minutes of your life";
cout<<endl;
return 0;
}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close