Uploaded by collin.weigert

Simulations&Modeling

advertisement
7.1)
function
Image_Out=Rotate90(Image_In)
[row, col]=size(Image_in);
for m=1:length(row)
for n=1:length(col)
Image_Out(n,m)=Image_In(m,n);
end
end
7.2)
T=[360 320 305 298 295 290 284
282 279 276];
R=[950 3100 4950 6960 9020
10930 13100 14950 17200 18950];
n=length(T)
a=zeros(3);
b=zeros(3,1);
a(1,1)=1;
a(1,2)=sum(log(T));
a(1,3)=sum(log(T).*log(T).*log(
T));
a(2,1)=a(1,2);
a(2,2)=sum(log(T).*log(T))
a(2,3)=sum(log(T).*log(T).*log(
T).*log(T));
a(3,1)=a(1,3);
a(3,2)=a(2,3);
a(3,3)=sum(log(T).*log(T).*log(
T).*log(T).*log(T));
b(1,1)=sum(1./(T+273.15));
b(2,1)=sum(1./(T+273.15).*log(R
));
b(3,1)=sum(1./(T+273.15).*log(R
).*log(R).*log(R));
C1C2C3=a\b;
C1=C1C2C3(1);
C2=C1C2C3(2);
C3=C1C2C3(3);
eqn=fprintf('1/(T+273.15)= %f +
%f *ln(R)+ %f *(ln(R))^3)', C1,
C2, C3);
disp(eqn)
7.3)
7.4)
function I1=Simpsons38(fun,a,b)
g=10*(b-a);
x=zeros;
y=zeros;
I1=zeros;
w=log(g)/log(2);
max=1;
for i=1:2000
if i<w
max=max+1;
end
end
for s=1:max
N=3*2^(s-1);
h=(b-a)/N;
for k=1:N+1
x(k)=a+(k-1)*h;
y(k)=fun(x(k));
end
S1=0;
Im=0;
for i=1:(N/3)
j=3*i-1;
S1=S1+y(j)+y(j+1);
end
S2=0;
for i=1:(N/3)-1
j=3*i+1;
S2=S2+y(j);
end
Im=Im+(3*h/8)*(y(1)+3*S1+2*S2+y
(N+1));
I1(s)=Im;
end
end
>> F=@(x) mpower(sin(x),2);
Simpsons38(F, 0, pi)
ans =
Columns 1 through 2
1.767145867644259
1.570796326794897
Columns 3 through 4
1.570796326794897
1.570796326794897
Column 5
1.570796326794896
>> Q=@(x) ((2*x)/(1+x^2));
Simpsons38(Q,0,2.4)
ans =
Columns 1 through 2
1.900055296012039 1.915454442805341
Columns 3 through 4
1.911335500466683 1.911038689709899
Column 5
1.911023839139797
7.5)
function I1=Simpsons38(Fun,a,b)
if a==b
I=0;
else
N=3;
h=(b-a)/N;
x=a:h:b;
y=Fun(x);
I=3*h/8*(y(1)+2*sum(y(4:3:(N2)))+y(N+1));
N=2*N;
check=0;
while check ==0;
h=(b-a)/N;
x=a:h:b;
y=Fun(x);
I_new=3*h/8*(y(1)+2*sum(y(4:3:(
N-2)))+y(N+1));
I_new=I_new+3*h/8*3*(sum(y(2:3:
(N-1)))+sum(y(3:3:N)));
error=abs((II_new)/I)*100;
if error>0.1
check=0;
N=N*2;
I=I_new;
end
end
end
end
function ef=ErrorFun(x)
F=@(t) exp(-t.^2);
n=length(x);
for i=1:n
if x(i)==Inf
ef(i)=1;
else
ef(i)=2/sqrt(pi)*Simpsons38(F,
0, x(i));
end
end
end
a)
x=0:0.02:2;
y=ErrorFun(x);
plot(x,y)
xlabel('x')
ylabel('y')
b)
x=0:0.02:2;
y=ErrorFun(x);
T=[x',y'];
disp('
x
erf(x)');
disp(T)
Download
Study collections