Figure 2.1.1
% percent sign introduces a comment in matlab
% an end of line completes a command, or semi-colon
% ends a command and surpresses printing results
C=(0:1:100); % C=vector of values from 0 to 100 by ones
F=(9/5)*C+32; % F=vector, this arithmetic to each C value
plot(C,F); % plot the F's vs the C's
xlabel('Temperature degrees C'); %label horizontal axis
ylabel('Temperature degrees F'); %label vertical axis
axis([-10,110,-30,220]);
% x scale from -10 to 110, y from -30 to 220
Page 13
% makeup a file named fig212.m with the following two
% lines (without the % signs); Matlab requires
% functions be defined in external files and finds
% them via the MATLAB PATH
% function y=fig212(x);
% y=x.^3 - 2.^x;
% resume this calculation
fzero('fig212',10) %no semi-colon to print answer
Page 14
x=linspace(0,14); %100 equally spaced values 0 to 14
y=100*x.^3./2.^x;
% .^ means term by term power,
% ./ and .* mean term by term division and multiplication
plot(x,y)
hold on % keep axis, scale, etc of the graph fixed
x=linspace(0,12);
plot(x,x.^3); % plot overlaid on the previous plot
plot(x,2.^x); % ditto
Exercises/Experiments
1A.
% some US to metric conversions
% Length: 1 inch = 2.54 cm (exactly)
% Mass: 1 lb = .45359237 kg (avoirdupois pound)
% Volume: 1 gallon = 3.785411784 liter (US gallon)
x=0:10; y=2.54*x; plot(x,y) % plot cm vs inch
% to plot kg/liter vs pounds/gallon one finds
% the number of the former per 1 of the latter;
% use this
% 1 lb/gal = (1 lb/gal)*(1 gal/3.78 lit)*(.453 kg/lb)
% cancel units so that
% 1 lb/gal = .45359237/3.785411784 kg/lit.
2B.
r=0:.1:1; % create vector of r values
plot(r,pi*r.^2)
% plot pi r squared vs r, use .^ (dot hat, not ^)
% to get term by term r square, no need for .*
% (dot star) since pi is a constant
hold on % to overlay this graph
plot(r,pi*(4/3)*r.^3);
hold off % begin new plot
loglog(r,pi*r.^2) % matlab automatically avoided r=0
hold on
loglog(r,(4/3)*pi*r.^3)
hold off
x=linspace(0,1); % divide 0 to 1 into 100 subdivisions
plot(x,3*5.^ x); hold on
plot(x,5*3.^ x)
3.
x=linspace(0,7); % vector of 100 x values
plot(x,3*x.^2+5*x+7); hold on
plot(x,2.^x)
% or make a matrix whose 1st row=polynomial
% and 2nd row=exponential
M=[3*x.^2+5*x+7; 2.^x];
plot(x,M) % and plot both at once
plot(x,M(1,:)./M(2,:))
% quotient of 1st row/2nd row term by term
% observe the limit is 0 graphically
4.
% for the 1st DE make an m-file, ex214a.m, say, containing
% function yprime=ex214a(t,y);
% yprime=3*y;
[t,y]=ode23('ex214a',[0 1],2);
plot(t,y)