'Enter First Value: ': 1 % Initialize Sums. % Read in First Value % While Loop To Read Input Values. % Accumulate Sums
'Enter First Value: ': 1 % Initialize Sums. % Read in First Value % While Loop To Read Input Values. % Accumulate Sums
'Enter First Value: ': 1 % Initialize Sums. % Read in First Value % While Loop To Read Input Values. % Accumulate Sums
% Initialize sums.
n = 0; sum_x = 0; sum_x2 = 0;
% Read in first value
x = input('Enter first value: ');
% While Loop to read input values.
while x >= 0
% Accumulate sums.
n = n+1;
sum_x = sum_x + x;
sum_x2 = sum_x2 + x^2;
% Read in next value
x = input('Enter next value: ');
end
% Calculate the mean and standard deviation
x_bar = sum_x / n;
std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) );
% Tell user.
fprintf('The mean of this data set is: %f\n', x_bar);
fprintf('The standard deviation is:%f\n', std_dev);
fprintf('The number of data points is: %f\n', n);
stats_1
% Initialize sums.
n = 0; sum_x = 0; sum_x2 = 0;
% Read in first value
x = input('Enter first value: ');
% While Loop to read input values.
while x >= 0
% Accumulate sums.
n= n+1;
sum_x = sum_x + x;
sum_x2 = sum_x2 + x^2;
% Read in next value
x = input('Enter next value: ');
end
% Check to see if we have enough input data.
if n<2 %Insufficient information
disp('At least 2 values must be entered!');
else % There is enough information, so
% calculate the mean and standard deviation
x_bar = sum_x / n;
std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) );
% Tell user.
fprintf('The mean of this data set is: %f\n', x_bar);
fprintf('The standard deviation is:%f\n', std_dev);
fprintf('The number of data points is: %f\n', n);
end
stats_2
doy
specified date.
specified date.
>> tugas3
specified date.
Ent
specified date.
4.
% Initialize sums.
sum_x = 0; sum_x2 = 0;
% Get the number of points to input.
n = input('Enter number of points: ');
% Check to see if we have enough input data.
if n<2
%Insufficient data
disp ('At least 2 values must be entered.');
else % we will have enough data, so let's get it.
% Loop to read input values.
for ii = 1:n
% Read in next value
x = input('Enter value: ');
% Accumulate sums.
sum_x = sum_x + x;
sum_x2 = sum_x2 + x^2;
end
% Now calculate statistics.
x_bar = sum_x / n;
std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) );
% Tell user.
fprintf('The mean of this data set is: %f\n', x_bar);
fprintf('The standard deviation is:%f\n', std_dev);
fprintf('The number of data points is: %f\n', n);
end
>> stats_3
Enter value: 4
Enter value: 5
Enter value: -1
>>
5.
>> timings
Vectorized= 0.0001
% Define variables:
% ii-- Loop index
% n_points-- Number in input [x y] points
% slope-- Slope of the line
% sum_x-- Sum of all input x values
% sum_x2-- Sum of all input x values squared
% sum_xy-- Sum of all input x*y yalues
% sum_y-- Sum of all input y values
% temp-- Variable to read user input
% x-- Array of x values
% x_bar-- Average x value
% y-- Array of y values
% y_bar-- Average y value
% y_int-- y-axis intercept of the line
disp('This program performs a least-squares fit of an ');
disp('input data set to a straight line.');
n_points = input('Enter the number of input [x y] points: ');
% Read the input data
for ii = 1:n_points
temp = input('Enter [x y] pair: ');
x(ii) = temp(1);
y(ii) = temp(2);
end
% Accumulate statistics
sum_x = 0;
sum_y = 0;
sum_x2 = 0;
sum_xy = 0;
for ii = 1:n_points
sum_x = sum_x + x(ii);
sum_y = sum_y + y(ii);
sum_x2 = sum_x2 + x(ii)^2;
sum_xy = sum_xy + x(ii) * y(ii);
end
% Now calculate the slope and intercept.
x_bar = sum_x / n_points;
y_bar = sum_y / n_points;
slope = (sum_xy - sum_x * y_bar) / ( sum_x2 - sum_x * x_bar);
y_int = y_bar - slope * x_bar;
% Tell user.
disp('Regression coefficients for the least-squares line:');
fprintf(' Slope (m) = %8.3f\n', slope);
fprintf(' Intercept (b)= %8.3f\n', y_int);
fprintf(' No. of points= %8d\n', n_points);
% Plot the data points as blue circles with no
% connecting lines.
plot(x,y,'bo');
hold on;
% Create the fitted line
xmin = min(x);
xmax = max(x);
ymin = slope * xmin + y_int;
ymax = slope * xmax + y_int;
% Plot a solid red line with no markers
plot([xmin xmax],[ymin ymax],'r-','LineWidth',2);
hold off;
% Add a title and legend
title ('\bfLeast-Squares Fit');
xlabel('\bf\itx');
ylabel('\bf\ity');
legend('Input data','Fitted line');
grid on
>> fitting_line
No. of points= 7
>>
Least-Squares Fit
8
Input data
Fitted line
7
5
y
7
% Define variables:
% conv-- Degrees to radians conv factor
% gravity-- Accel. due to gravity (m/s^2)
% ii, jj-- Loop index
% index-- Location of maximum range in array
% maxangle-- Angle that gives maximum range (deg)
% maxrange-- Maximum range (m)
% range-- Range for a particular angle (m)
% time-- Time (s)
% theta-- Initial angle (deg)
% traj_time-- Total trajectory time (s)
% vo-- Initial velocity (m/s)
% vxo-- X-component of initial velocity (m/s)
% vyo-- Y-component of initial velocity (m/s)
% x-- X-position of ball (m)
% y-- Y-position of ball (m)
% Constants
conv = pi / 180; % Degrees-to-radians conversion factor
g = -9.81;
% Accel. due to gravity
vo = 20;
% Initial velocity
%Create an array to hold ranges
range = zeros(1,91);
% Calculate maximum ranges
for ii = 1:91
theta = ii - 1;
vxo = vo * cos(theta*conv);
vyo = vo * sin(theta*conv);
max_time = -2 * vyo / g;
range(ii) = vxo * max_time;
end
% Write out table of ranges
fprintf ('Range versus angle theta:\n');
for ii = 1:91
theta = ii - 1;
fprintf(' %2d %8.4f\n',theta, range(ii));
end
% Calculate the maximum range and angle
[maxrange index] = max(range);
maxangle = index - 1;
fprintf ('\nMax range is %8.4f at %2d degrees.\n',...
maxrange, maxangle);
% Now plot the trajectories
for ii = 5:10:85
% Get velocities and max time for this angle
theta = ii;
vxo = vo * cos(theta*conv);
vyo = vo * sin(theta*conv);
max_time = -2 * vyo / g;
% Calculate the (x,y) positions
x = zeros(1,21);
y = zeros(1,21);
for jj = 1:21
time = (jj-1) * max_time/20;
x(jj) = vxo * time;
y(jj) = vyo * time + 0.5 * g * time^2;
end
plot(x,y,'b');
if ii == 5
hold on;
end
end
% Add titles and axis lables
title ('\bfTrajectory of Ball vs Initial Angle \theta');
xlabel ('\bf\itx \rm\bf(meters)');
ylabel ('\bf\ity \rm\bf(meters)');
axis ([0 45 0 25]);
grid on;
% Now plot the max range trajectory
vxo = vo * cos(maxangle*conv);
vyo = vo * sin(maxangle*conv);
max_time = -2 * vyo / g;
% Calculate the (x,y) positions
x = zeros(1,21);
y = zeros(1,21);
for jj = 1:21
time = (jj-1) * max_time/20;
x(jj) = vxo * time;
y(jj) = vyo * time + 0.5 * g * time^2;
end
plot(x,y,'r','LineWidth',3.0);
hold off
>> ball
0 0.0000
1 1.4230
2 2.8443
3 4.2621
4 5.6747
5 7.0805
6 8.4775
7 9.8643
8 11.2390
9 12.6001
10 13.9458
11 15.2745
12 16.5846
13 17.8745
14 19.1426
15 20.3874
16 21.6073
17 22.8009
18 23.9668
19 25.1034
20 26.2095
21 27.2836
22 28.3245
23 29.3309
24 30.3015
25 31.2352
26 32.1309
27 32.9874
28 33.8038
29 34.5789
30 35.3119
31 36.0019
32 36.6481
33 37.2496
34 37.8057
35 38.3157
36 38.7791
37 39.1952
38 39.5635
39 39.8837
40 40.1553
41 40.3779
42 40.5514
43 40.6754
44 40.7499
45 40.7747
46 40.7499
47 40.6754
48 40.5514
49 40.3779
50 40.1553
51 39.8837
52 39.5635
53 39.1952
54 38.7791
55 38.3157
56 37.8057
57 37.2496
58 36.6481
59 36.0019
60 35.3119
61 34.5789
62 33.8038
63 32.9874
64 32.1309
65 31.2352
66 30.3015
67 29.3309
68 28.3245
69 27.2836
70 26.2095
71 25.1034
72 23.9668
73 22.8009
74 21.6073
75 20.3874
76 19.1426
77 17.8745
78 16.5846
79 15.2745
80 13.9458
81 12.6001
82 11.2390
83 9.8643
84 8.4775
85 7.0805
86 5.6747
87 4.2621
88 2.8443
89 1.4230
90 0.0000
>>
20
15
)