I am Boris M. I am a Computer Science Assignment Help Expert at programminghomeworkhelp.com. I hold MSc. in Programming, McGill University, Canada. I have been helping students with their homework for the past 7 years. I solve assignments related to Computer Science.
Visit programminghomeworkhelp.com or email support@programminghomeworkhelp.com.You can also call on +1 678 648 4277 for any assistance with Computer Science assignments.
1. For any help regarding Programming Homework Help
Visit :- https://www.programminghomeworkhelp.com/ ,
Email :- support@programminghomeworkhelp.com or
call us at :- +1 678 648 4277
programminghomeworkhelp.com
2. Question (1):
(a) Write a Matlab M-file which generates a table of error function (erf) and its derivatives
for real arguments (z) between -3 and 3 in steps of 0.25. The error function is defined by
the equation below (but is rarely evaluated by performing the integration).
The values in the table should be given with 5 decimal places. The table should have
headers explaining what the columns are. Explain how you designed the M-file and give
an example of the output.
(b) How would you change this M-file if 10 significant digits were required? Matlab M-file
should also be supplied
Question (2):
Write an M-file that reads your name in the form <first name> <middle name> <last
name> and outputs the last name first and adds a comma after the name, the first name,
and initial of your middle name with a period after the middle initial. If the names start
with lower case letters, then these should be capitalized. The M-file should not be
specific to the lengths of your name (ie., the M-file should work with anyone’s name.
Problems
programminghomeworkhelp.com
3. As an example. An input of thomas abram herring would generate: Herring, Thomas A.
Question (3):
Write a Matlab M-file that will compute the motion of a bicyclist and the energy used
cycling along an oscillating, sloped straight-line path. The path followed will be expressed
as
where H(x) is the height of the path above the starting height, S is a slope in m/m, A and
B are amplitudes of sinusoidal oscillations in the path. The wavelength of the oscillations is
λ. The forces acting on the bicycle are:
where Ar is the cross-sectional area of the rider, Cd is the drag coefficient, r is the density
of air and V is the velocity of the bike. For the rolling drag, Mr is the mass of the rider and
bike, g is gravitation acceleration and Cr is rolling drag coefficient.
The bicyclist puts power into the bike by pedaling. The force generated by this power is
given by
where Fr is the force produced by the rider, Pr is power used by the rider and V is
velocity that the bike is traveling (the force is assumed to act along the velocity vector of the
bike).
programminghomeworkhelp.com
4. Your M-file can assume that the power can be used at different rates along the path. The
energy used will be the integrated power supplied by the rider. Assume that there is
maximum value to the rider force.
Your code should allow for input of the constants above (path and force coefficients). The M-
file can assume a constant power scenario and constant force at low velocities.
As a test of your M-file use the following constants to compute:
(a) Time to travel and energy used to travel 10 km along a path specified by S=0.001, A=5.0
m, B=0.0 m and λ= 2km, with constant power use of Pr =100Watts and a maximum
force available of 20N.
(b) The position and velocity of the bike tabulated at a 100-second interval.
(c) Add graphics to your M-file which plots the velocity of the bike as a function of time and
position along the path.
Assume the following values
Cd = 0.9
Cr = 0.007
A r = 0.67 m 2
r = 1.226 km/m3
g = 9.8 m/s2
Mr = 80 kg programminghomeworkhelp.com
5. In this case, the Matlab M-file will not be of the type used for fortran and C/C++. Look at
the documentation on ODExx where xx is a pair of number for Ordinary Differential Equation
solutions. Your answer to this question should include:
(a) The algorithms used and the design of your M-file
(b) The Matlab M-file with your code and solution (I run your M-file).
(c) The results from the test case above.
programminghomeworkhelp.com
6. Solutions
Question 1:
The solution to this problem is easy in Matlab because the Erf function is built in. In the
solution we use Matlab’s vector operations to form the entries for the table in one single line.
By using the correct transpose on the results we are able to print the table with one print
statement. We also added to the solution plots of the function and its derivative.
% HW5_1: Matlab script output Erf and its derivative
%
arg = -3:0.25:3;
% Make a table with the results in it
Results = [ arg ; erf(arg) ; 2/sqrt(pi)*exp(-arg.^2) ];
fprintf('n12.010 HW05 Q01: Table of Erf and its derivativen');
fprintf('-------------------------------------n');
fprintf('| Arg x | Erf(x) | d(Erf)/dx |n');
fprintf('-------------------------------------n');
fprintf('| %7.3f | %10.5f | %10.5f |n',Results);
fprintf('-------------------------------------n');
%% Create Figure of results
% Regenerate results at finer spacing.
figure ;
arg = -3:0.05:3;
programminghomeworkhelp.com
7. Results = [ arg ; erf(arg) ; 2/sqrt(pi)*exp(-arg.^2) ];
plot(Results(1,:),Results(2,:),'k',Results(1,:),Results(3,:),'b');
xlabel('Argument'); ylabel('Erf dErf/dx');
legend('Erf','dErf/dx','Location','NorthWest') axis tight;
Below is shown in the output from the program for 5 significant digits. To increase the
number of significant digits, the format for the table simply needs to be changed. All of the
calculations were done with double precision and therefore no change to the variables
needs to be made.
programminghomeworkhelp.com
8. Figure 1. Plot of the Erf function and its derivative.
programminghomeworkhelp.com
9. Question (2):
This solution is also easy in Matlab using the tolower and toupper functions. The code
also detects whether two or three names have been given and changes the output
accordingly.
% HW5_2: Matlab script file to rearrange names.
%
% Ask user for name
fprintf('n----------------------------------------n');
name = input('12.010 HW 05 Q2:nEnter Name (first middle last) ','s');
%
% Convert to lower case so that we know state name = lower(name);
%
% Find the blanks in the sting
blanks = strfind(name,' ');
if length(blanks) == 1
% Only two names passed, so skip middle initial
first = name(1:blanks(1)-1);
first(1) = upper(first(1));
last = name(blanks(1)+1:length(name));
last(1) = upper(last(1));
fprintf('%s, %s n',last, first)
programminghomeworkhelp.com
10. elseif length(blanks) == 2
% Split of the names. The function strtok could also be used to split
% the string into parts.
first = name(1:blanks(1)-1);
middle = name(blanks(1)+1:blanks(2)-1);
last = name(blanks(2)+1:length(name));
first(1) = upper(first(1));
middlei = upper(middle(1));
last(1) = upper(last(1));
fprintf('%s, %s %s.n',last, first, middlei)
else
fprintf('Too many names or spaces in %snProgram endn',name)
End
----------------------------------------
12.010 HW 05 Q2:
Enter Name (first middle last) thomas abram herring
Herring, Thomas A.
programminghomeworkhelp.com
11. Question 3:
The solution to this problem is also quite easy in Matlab. We use the OED 45 1st order
differential equation solver. To use this function we also have a function that computes the
acceleration at the bike and another function that is the event detector that determines
when the bike reaches 10 km distance. For ease of coding, the variables that control the
characteristics of the bike are saved as globals. We also use an input dialog box to both set
the defaults and allow the user to change the values at runtime. Also included in the
solution is an animation and plot of the motion of the bike. To have the animation runs
smoothly we change the output interval of the differential equation solver so that we
generate more points then those that given in the standard 100 seconds separated values.
There are some interesting bugs in Matlab for plotting the velocity vectors. The quiver
option is used for this plot but in the current release of Matlab the arrowheads are
extremely large and are turned off in the plots. Also the automatic scaling of the vectors
does not generate reasonable length vectors, and so in these plots we manually set the
scale factor.
To compute the energy used during a bike ride, we simply added the derivative of the
energy to the differential equations. This way the total energy was integrated automatically
in the solution and could be output to the table as shown below.
programminghomeworkhelp.com
12. The solution M-files are:
% 12.010 HW 05 Q 3: Bike simulator
%
% This script m-file simulates the motions of a paper plane being
% influenced by gravity, lift and drag
%
% There are many ways to solve this problem and we will use the Matlab ODE
% solvers with event detection (the event in this case is the z-value
% hitting zero).
%
global Grav Rho Mass Cd Cr Area Slope As Bs Lambda Pr Fr_Max TLength terr % Define
these globals so that they will be accessible in the acceleration routine
global Grav Rho Mass Cd Cr Area Slope As Bs Lambda Pr Fr_Max TLength terr % Define
these globals so that they will be accessible in the acceleration routine
Grav = -9.8 ; % m/sec^2
Rho = 1.226; % kg/m^3
%----- Get the inout that we need -----
prompt = {'Track Grade (m/m)', 'Track Asin (m)', 'Track Bcos (m)', 'Wavelegth
(km)','Track Length (km)','Mass (kg)', ...
'Rider Area (m^2)', 'Drag Coefficient','Rolling coefficient','Power
programminghomeworkhelp.com
13. (watts)','Max Force (N)', ...
'Output Interval (sec)','Error (mm)'};
title = 'Track and Bike Parameters';
nlines = 1;
defaults =
{'0.001','5.0','0.0','2.0','10.0','80.0','0.67','0.9','0.007','100.0','20','100','1'};
% Get user inputs and save in the appropriate valuyes result =
inputdlg(prompt,title,nlines, defaults,'on');
%
Slope = eval(result{1});
As = eval(result{2});
Bs = eval(result{3});
Lambda = eval(result{4})*1000.0 ; % Convert to meters
TLength = eval(result{5})*1000.0 ; % Convert to meters
Mass = eval(result{6}) ;
Area = eval(result{7}) ;
Cd = eval(result{8}) ;
Cr = eval(result{9}) ;
Pr = eval(result{10}) ;
Fr_Max = eval(result{11}) ;
tstep = eval(result{12}) ;
terr = eval(result{13})/1000 ; % convert height error to m
programminghomeworkhelp.com
14. %
% Set the ODE options. hit is the name of an m-file which will detect when % we hit the
ground.
options = odeset('AbsTol',[terr terr terr terr 100],'Events','bikehit');
% Set up the initial conditions
y0 = [0.0; Bs ; 0.0 ; 0.0; 0.0];
% Set the maximum integration time to 5000 seconds. In a more general
% problem we should check to see of this is enough time
tmax = 5000;
% Solve the ODE
fprintf('n Starting new run n');
[t,y,te,ye,ie] = ode45(@bikeacc,[0:tstep:tmax],y0,options);
%
% Output the final results fprintf('---------------------------------------------------------------------n');
fprintf('12.010 HW 05 Q 3: Bike simulationn');
% Output table of values
fprintf('--------------------------------------------------------------------------n')
fprintf(' Time X pos Z pos X Vel Z Vel Energy n')
fprintf(' (sec) (m) (m) (m/s) (m/s) (Joules)n')
fprintf(' %9.3f %12.4f %10.4f %10.4f %10.4f %12.2fn',[t,y(:,1:5)]');
programminghomeworkhelp.com
15. fprintf('-------------------------------------------------------------------------------------------------n')
fprintf(‘ Time : %8.3f, Velocity %6.4f %6.4f n',te,ye(3),ye(4))
fprintf('Energy used: %12.2f Joules, %8.2f kcalsn', ye(5), ye(5)*0.2388e-3);
% Now animate (make as function)
%banimate(t,y);
% Re-run the solution for the animation
[ta,ya,tae,yae,iae] = ode45(@bikeacc,[0:10.0:te],y0,options);
fprintf('nTo show animation: banimate(ta,ya)n');
%
function dy = bikeacc(t, y)
% acc: Computes accelerations including drag (Cd), rolling (Cr) and
% and gravity
% y contains, [x z xdot zdot energy] and dy is the time derivative
%
global Grav Rho Mass Cd Cr Area Slope As Bs Lambda Pr Fr_Max TLength % Define these globals
so that they will be accessible in the acceleration routine
dy = zeros(5,1); % a column vector
%
% get the road slope for our current x position.
programminghomeworkhelp.com
16. theta = atan(Slope + As*cos(2*pi*y(1)/Lambda)*2*pi/Lambda -
Bs*sin(2*pi*y(1)/Lambda)*2*pi/Lambda);
%
% Compute forces: Assuming bike remains on road surface
% Drag:
vmag = sqrt(y(3)^2+y(4)^2); thetad = atan2(y(4),y(3));
dxd = -Rho*Cd*vmag^2*Area*cos(thetad)/(2*Mass);
dzd = -Rho*Cd*vmag^2*Area*sin(thetad)/(2*Mass);
dxr = +Grav*Cr*cos(theta) ;
dzr = +Grav*Cr*sin(theta) ;
%
% Get rider force
Fr_mag = Fr_Max ;
if ( vmag > 0 ) Fr_mag = Pr/vmag ;
end
if ( Fr_mag > Fr_Max ) Fr_mag = Fr_Max ; end
height = Slope*y(1) + As*sin(2*pi*y(1)/Lambda) + Bs*cos(2*pi*y(1)/Lambda);
%if ( y(2) > height + 0.1 ) Fr_mag = 0; end
dxp = Fr_mag*cos(theta)/Mass ;
dzp = Fr_mag*sin(theta)/Mass ;
% % Normal force from surface
(Grav is a negative value)
dxn = Grav*cos(theta)*sin(theta); programminghomeworkhelp.com
17. dzn = -Grav*cos(theta)^2;
% Get the curvature
dy2dx2 = -As*sin(2*pi*y(1)/Lambda)*(2*pi/Lambda)^2 -
Bs*cos(2*pi*y(1)/Lambda)*(2*pi/Lambda)^2;
IRc = (dy2dx2)/(1+theta^2)^(3/2);
dxc = vmag^2*sin(theta)*IRc;
dzc = vmag^2*cos(theta)*IRc;
%fprintf(' %5.2f %9.5f %9.5f %5.2f %6.4f %10.1f n',vmag, dxc, dzc, dxd, dzd, Rc)
%
% Update the dy velocity acceleration vector
dy(1) = y(3);
dy(2) = y(4);
dy(3) = dxd + dxr + dxp + dxn + dxc ;
dy(4) = dzd + dzr + dzp + dzn + dzc + Grav ;
dy(5) = Fr_mag * vmag;
function [value,isterminal,direction] = bikehit(t,y)
% Locate the time when height passes through zero in a decreasing direction
% and stop integration.
global Grav Rho Mass Cd Cr Area Slope As Bs Lambda Pr Fr_Max TLength
value = y(1)-TLength; % detect when end is 0.
isterminal = 1; % stop the integration
direction = 1; % negative direction
programminghomeworkhelp.com
18. function banimate(t,y)
% Function to animate the motion
qs = 50;
num = length(t);
hf1 = figure(1); set(hf1, 'Position',[10 100 750 400]); hold off;
pc = plot(y(1,1),y(1,2),'o','MarkerFaceColor','r','EraseMode','xor'); hold on
pq = quiver(y(1,1),y(1,2),y(1,3),y(1,4),0);
xm = y(num,1)*1.01; ym = max(y(:,2))*1.01;
xs = min(y(:,1))*1.01; ys = min(y(:,2))*1.01;
xlim([xs xm]); ylim([ys ym]);
xlabel('Distance (m)'); ylabel('Height (m)');
pt = text(xm/100,ym*0.96,'Time 0.00 s');
%daspect([1 1 1]);
drawnow;
% Release 2011a of Matlab has some strange scaling in quiver and so we
% manually set properities that should be automatically set. Also arrow
% heads are very wide so we turn them off here.
for i = 1: num
% Replace the data point
set(pc,'XData',y(i,1),'YData',y(i,2));
delete(pq); pq = quiver(y(i,1),y(i,2),y(i,3),y(i,4),qs);
set(pq,'ShowArrowHead','off','AutoScaleFactor',qs,'AutoScale','on');
otext = sprintf('Time %6.1f s',t(i));
programminghomeworkhelp.com
19. set(pt,'String',otext);
drawnow;
end
plot(y(:,1),y(:,2),'g');
%%
hf2 = figure(2); set(hf2, 'Position',[760 100 350 200]);
hold off; pc = plot(y(:,1),y(:,2),'o','MarkerFaceColor','r','EraseMode','normal'); hold on pq =
quiver(y(:,1),y(:,2),y(:,3),y(:,4)); set(pq,'ShowArrowHead','off'); xlabel('Distance (m)');
ylabel('Height (m)'); axis tight; ylim([-10 15]);
%%
hf3 = figure(3); set(hf3, 'Position',[760 380 350 200]);
plot(t,(sqrt(y(:,3).^2+y(:,4).^2)));
xlabel('Time (sec)'); ylabel('Total Velocity (m/s)');
The table and figures below show the results for the default solution.
programminghomeworkhelp.com
21. Figure 2: Trajectory of the bike along with vectors showing its velocity (green
lines). Due to poor scaling in Matlab, no arrowheads are shown.
Figure 3: Velocity as a function of time for the bike.
programminghomeworkhelp.com