CSUN COMP 106 - Developing Multifunction Programs

Unformatted text preview:

Project two – multifunction development April 18, 20061Second Programming Project Second Programming Project ––Developing Multifunction Developing Multifunction ProgramsProgramsLarry CarettoComputer Science 106Computing in Engineering and ScienceApril 18, 20062Outline• Goals for second project• Specific tasks for second project– Evaluation of infinite series that has more complicated terms than the one for sin(x) in exercise six– Trial and error solution of one equation– Combination of first two tasks• Code structure for project two tasks3Project Two Goals• Develop and test code with multiple functions in a stepwise manner• Compute more complex infinite series than those in exercise six• Solve one equation in one unknown by trial and error• View one parameter in an infinite series as an unknown4Project Two Overview• Write and test a function to compute the temperature in a slab as a function of time, T(z,t)• Write and test a function to solve an equation in one variable x = 3sin(x) + cos(x)• Combine the first two functions to find the time when the temperature at a given z location in a slab achieves a certain value5Temperature Problem• Have a slab that with large dimensions in x and y directions (relative to z)• Look at one dimensional heat conduction in z direction (-L ≤ z ≤ L)• Initially entire slab is at temperature T0• At t = 0 sides (z = ±L) are set to TB• Want to find T(z, t)6Solution•T0, TB, L, z, t, and α (α = thermal diffusivity) are given as input data• Find T(z, t) from infinite series, similar to exercise six)cos()1(21),(2000ξλλτλnnnnBneTTTtzT−∞=∑−−=−−()2212LtLznnατξπλ==+=Project two – multifunction development April 18, 200627Summing Infinite Series• Define allowed error and maximum terms• Set the series sum to zero• Loop while not converged and number of terms is less than maximum– Compute new term in series– Add new term to sum– converged = |term| <= allowErr * |sum|•End loop8Task One•Writedouble temperature( double z, double time) to compute T(z,t )• Use global variables for T0, TB, L, and α• Write main function that calls your temperature function in a nested loop to compute table for T(z, t)– Recall exercise six, tasks one and two• Plot results to estimate t100, defined as time such that T(0,t100) = 100• Sample results on next chartSlab Temperature Profile050100150200250300350400450-0.1 -0.08 -0.06 -0.04 -0.02 0 0.02 0.04 0.06 0.08 0.1Distance, z (m)Temperature (C)500 s1000 s2000 s3000 s10Trial-and-error Solutions• What is solution of 3x + 7 = 2x + 3?• What is solution of x = 3 sin(x) + cos(x)?• Second case has only one unknown, but cannot be solved explicitly• Need a process for solving such equations• General problem: find x so that f(x) = 0• Here f(x) = x – 3 sin(x) – cos(x) = 0– Could also write f(x) = 3sin(x) + cos(x) – x = 0, but it would have the same solutions11f(x) = x – 3 sin(x) – cos(x)-4-2024-3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3xf(x)• What values of x give f(x) = 0?12Task Two• Write a function double secant( double x ) that solves f(x) = 0 in general• Algorithm in project description• Upon entry, x is initial guess; final x (or error code) returned in function name• Function will apply to any problem for solving f(x) = 0 if we write f(x) as a separate functionProject two – multifunction development April 18, 2006313Secant Function Algorithm• Uses two most recent guesses x1and x2to compute new guess, x3, based on values of f1= f(x1) and f2= f(x2)• Uses linear interpolation to get f(x3) ≈ 0•x3= x1+ [(x2–x1)/(f2–f1)] (0 –f1)• Interpolation not exact; iterate to obtain small error• Adjust x3to avoid large change• Pseudocode in assignment14Task Two Code• Three functions– Main function gets user input on initial guess, calls secant function and prints result– Secant function to solve f(x) = 0 calls function below to evaluate equationdouble f( double x ){return x – 3 * sin( x ) – cos( x );}15Task Two Code• Three functions– Main function gets user input on initial guess, calls secant function and prints result– Secant function to solve f(x) = 0 calls function below to evaluate equationdouble f( double t ){return t – 3 * sin( t ) – cos( t );}Note that this function gives same result as one on last slide16Task Three• Combination of tasks one and two– In task one we found temperature for given time (and other variables)– Here we use secant method from task two to find (unknown) time to reach given temperature (all other data given)– Use temperature and secant functions from tasks one and two without changeSlab Temperature Profile050100150200250300350400450-0.1 -0.08 -0.06 -0.04 -0.02 0 0.02 0.04 0.06 0.08 0.1Distance, x (m)Temperature (C)500 s1000 s2000 sUnknow n3000 s18Task Three Code• Main function gets initial guess from user, calls secant function to get answer and prints answer• Secant and temperature functions from tasks one and two used here• Create new f(x) function that gives time so that f(time) = 0 when computed temperature equals desired temperatureProject two – multifunction development April 18, 2006419f(time) functionconst double ERROR_FLAG = -999.999;double desiredT;double f(double time){double const centerlineZ = 0;double temp = temperature( centerlineZ, time );if( temperature == ERROR_FLAG )return ERROR_FLAG;elsereturn temp - desiredT;}20Task Three Summary• Main function (similar to task two)– get initial guess on time from user– call secant function to get answer– print result• Temperature function from task one• Secant function from task two• New f(time) function from previous chart• Download answers from web site and match them before submitting project21Programming Guidelines• Download from web site and use in last three assignments• Designed to make programs easier to understand and modify• Generally simple set of priciples• Summary on next chart22Programming Guidelines II• Good program structure: program logic steps easy to follow• Effective use of comments: help explain program but not excessive• Well-formatted output• Neat appearance of code• Meaningful variable names• Portability and documentation: Not considered in Comp 10623Programming Guidelines II• Good program structure: This means code that is inherently easy to read and understand. Well-structured programs have a control flow that can


View Full Document

CSUN COMP 106 - Developing Multifunction Programs

Download Developing Multifunction Programs
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Developing Multifunction Programs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Developing Multifunction Programs 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?