Professional Documents
Culture Documents
EXERCISE # 2
MATLAB
1.% Factorial Function.
>> n=12;
>> y=1;
>> for i=1:n;
y=y*i;
end
>> disp(y)
479001600
2.%Sum of squares Function.
>> N=100;
>> S=0;
>> for i=1:N;
S=S+i^2;
end
>> disp(S)
338350
subplot(2,2,2);
plot(x,y2);
subplot(2,2,3);
plot(x,y3);
subplot(2,2,4);
plot(x,y4);
output.
1
1
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
0
0.5
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
0.5
0.5
0.5
Output
Columns 66 through 78
0.0617
0.0625
0.0631
0.0528
0.0505
0.0480
0.0100
0.0060
0.0020
Columns 79 through 91
Columns 92 through 99
0.0291
0.0254
0.0217
>> max(y)
ans =
>> min(y)
ans =
-0.9995
1.differential equation
N=100;
dx=10/(N-1);
x=0:dx:10;
x0=5;
y=exp(-(x-x0).^2)
diffy=diff(y)
0.0179
0.0140
Output
Columns 66 through 78
Columns 79 through 91
Columns 92 through 99
>> max(y)
ans =
0.9975
>> min(y)
ans =
1.3888e-011