

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Fall 2007
Objective: Write a ‘newtsqrt’ function to compute the square root of a number, using Newton’s algorithm. MATLAB Code function r = newtsqrt(x) % newtsqrt: computes square root of x using Newton's method % % Synopsis: r = newtsqrt(x) % % Input: x = number to find square root of % % Output: r = approximation to square root of x % --- initialize variables delta = 10^-6; r = x; rold = x + 2delta; maxit = 100; % --- iterate for square root for it = 1:maxit rold = r; r = 0.5(rold + x/rold); if abs(rold - r) < delta break end end Results
newtsqrt(4) ans =
newtsqrt(5) ans =
newtsqrt(1024) ans =
Objective Write two function m-files FtoC and CtoF, to convert temperatures between degree Fahrenheit and degrees Celsius. Each function should have one input and one output. MATLAB Code function [TdegC] = FtoC(TdegF) % FtoC: converts temp in deg F to deg C % % Synopsis: [TdegC] = FtoC(TdegF) % % Input: TdegF = temperature in deg F % % Output: TdegC = temperature in deg C TdegC = (TdegF -32)5/9; function [TdegF] = CtoF(TdegC) % CtoF: converts temp in deg C to deg F % % Synopsis: [TdegF] = CtoF(TdegC) % % Input: TdegC = temperature in deg C % % Output: TdegF = temperature in deg F TdegF = TdegC9/5 + 32; Results
FtoC( CtoF(100) ) ans = 100 CtoF( FtoC(32) )
ans = 32
FtoC( linspace(32,212,11) ) ans = 0 10 20 30 40 50 60 70 80 90 100 CtoF(0:10:100) ans =