ScientificProgrammingexamps

advertisement
Scientific Programming
A MATLAB/C++ Approach to a
Computer Science Course for Science
and Engineering Students
Discussion:
This course is designed for students who will only take one programming course for their major.
MATLAB is used in the first half of the course to introduce students to the logical constructs of imperative programming in the context of applications for science and
engineering. The second half of the smester students are introduced to C++, with an emphasis on how it differs from MATLAB and also the common constructs. A brief
introduction to classes is included. All is done again in the context of applications. The semester ends with a final project that links C++ and MATLAB.
Examples:
The Visual
Application
Fit a sin curve to monthly average
high temperature for a given city.
Purpose
MATLAB graphing.
curve fitting.
MATLAB arrays, expressions, functions
Comments/Code/etc.
Parabolic motion (first example),
Parabolic motion with bounce
(jazzing it up)
User input
Conditional statements (where increasing,
decreasing)
More MATLAB graphing, functions
t4 = t1 + t2
checktime = input('enter time between 0 and t4 ');
if (checktime < t1)
if ((v0-9.8*checktime)>0)
disp('going up1');
else
disp('coming down1');
end;
else
if((v0/2-9.8*(checktime-t1))>0)
disp('going up');
else
disp('coming down');
end;
end;
Heat to raise water’s temperature
Flowcharts, nested conditional statements
Color coded traces of input in
flowchart
Center of Mass
MATLAB array operations
MATLAB graphing
Simple loops
%program to show the center of mass for increasing
number of weights
x1=[];%initial conditions
y1=[];
h=[];
for i=1:4
x1(i)= input('enter x position of next weight');
y1(i)= input('enter y position of next weight');
h(i)= input('enter next weight');
totalwt=sum(h)
u=sum(h.*x1)/totalwt %x coordinate center of
mass
v=sum(h.*y1)/totalwt %y coordinate center of
mass
stem3(x1,y1,h,'fill');%graph weights
hold on;
stem3(u,v,0,'g','fill');%graph center of mass
end
Lamb growth/survival data analysis
based on number of lambs per ewe.
More complex program involving nested
control structures, more variables
Students wrote their own version
of this for their own data
% fits a sin curve to weather data
mnthmax = [ 33.6 36.9 48.9 60.3 70.5 78.8
82.6 80.8 74.3 62.4 50.4 38.5];
yearmax = max(mnthmax)
yearminmax=min(mnthmax)
period =12;
amp = (yearmax-yearminmax)/2
center = amp + yearminmax
t =[1:0.1:24];
maxtemps=amp*sin(2*pi*(t-4)/12)+center;
plot(t,maxtemps)
hold on;
t1=[1:12];
plot(t1,mnthmax,'or')
plot([13:24],mnthmax,'or')
hold off;
title('Pittsburgh''s average monthly high
temperatures');
Numerous examples:
Conditional statements
The longest length sequence of U’s in a Loops
nucleotide sequence.
Functions
Converting a sequence of numcleotides tot
to a sequences of amino acids.
Name that codon!
Number of each nucleotide type.
%function to detrmine the number of A's , C's, G's
and U's in a sequence of nucleotides D
function [As,Cs,Gs,Us] = numo(D)
As=0;
Cs=0;
Gs=0;
Us=0;
for i = 1:length(D)
if (D(i)== 'A')
As = As+1;
elseif (D(i)== 'C')
Cs = Cs+1;
elseif (D(i)== 'G')
Gs = Gs+1;
elseif (D(i)== 'U')
Us = Us+1;
end;
end;
The MATLAB Half of the course
MATLAB (matrix laboratory) is a popular scripting program for engineers and science and there are many science and engineering
applications packages available in MATLAB. It seamlessly processes arrays and has a good GUI for graphing. For example, to multiply
two same sized arrays, u and v, termwise, one types:“u.*v “ at the command line. To see which elements in an array, v, are greater than
10, type: “ a = (v > 10) “. The variable a is then an array of zeros and ones. “sum(a)” would tell how many elements of v are greater than
10. The commands: “x = [0:0.02: 2*pi]; y = sin(x); plot(x,y)” declare x and y as two arrays of size 315 and plots y=sin(x) as a connected
graph. Students learn to think of single valued variables as arrays with one element, and of arrays as objects where one must take care
with operations and indexing.
With the easy user interface, students can get quickly involved in solving application problems using MATLAB. With that as
motivation, we show them they can solve even more involved and useful problems by adding MATLAB programming techniques. These
include the standard conditional constructs, loops, functions, file input, structs.
The above programming concepts are demonstrated through a series of applications Yet these applications also have the purpose of
stepping the students through the layers between applied problem formulation, conceptual solution, and program solution in the context
of a relatively kind scripting language. The applications include
*curve fitting: a sin curve fit to weather data
bezier curves
interpolation
least squares method
parametric curves
*parabolic motion: graphing
determining when the object is going up and when it is going down
parabolic motion with bounce
*center of mass
*stattistical analysis of data
*molecular weight
*radioactive decay
*analysis of biological data by groupings
*heat to raise the temperature of water
*DNA analysis
Our goal is to expand this list to be able to tailor the class to its population and to also weave variations on a problem throughout the
course.
The MATLAB portion closes with a lecture on floating point representation and error.
The C++ Half of the Course
The second half of the semester reinforces the programming concepts learned in the first half, but now in the context of C++. Seeing
programming constructs in two languages is very informative to the students. Referrals are made to some of the MATLAB examples and
many are translated to C++ and then expanded upon. Other examples are also developed. Care has to be taken with data types and
operations on arrays generally have to be done with loops. More in depth programs are asked of students, since this is the second time thy
have seen most of the constructs. Modularity will be implemented more. Examples include simulation-one example is to create 10
random RNA samples and then analyzing their differences, linear regression-tree growth rate as a linear function of rainfall, Classes will
be introduced briefly near the end of the semester. A semester end project will combine MATLAB and C++. The students also learn the
operating system Linux in this half.
General notes
The course is a three hour course and has all its meetings in a computer classroom. Students are encouraged to type along on their
computers as the professor demonstrates examples. There are small programming exercizes for both in and out of class. Additionally
larger projects are both worked on in class and assigned as homework.
Students reactions to having applications have been exceptionally favorable. The majority of students are faring well with learning the
logical constructs and programming.
Students do a significant portion fo their programming in groups of two. This has no negative effect on their performance on exams or
their programming individually. Working in groups helps relieves frustration of learning the syntax and actually increases confidence
References
Fan& Schwartz, Introductory Programming Using MATLAB:
http://www.mathworks.com/company/newsletters/news_notes/oct02/programming.html
Etter&Ingber, Engineering Problem Solving with C++, 2003, Prentice Hall.
Kaplan, Introduction to Scientific Computation and Programming, 2004, Thomson Learning.
Course web page:
http://euler.slu.edu/%7Edruschel/csa145/ScientificProg.html
Download