正在加载图片...
2.4.2 Evaluating a Function of Two Variables Write a MATLAB program to evaluate a function fx.y)for any two user-specified values x and y.The function fx.y)is defined as follows. x+yx≥0 and y≥0 x+y2x≥0 and y<0 f(x,y)= x2+y x<0 and y≥0 x2+y2x<0 and y<o The function f(xy)is evaluated differently depending on the signs of the two independent variables x and y To determine the proper equation to apply,it will be necessary to check for the signs of the x and y values supplied by the user. 1.State the problem Evaluate the function f.y)for any user-supplied values of x and y. 2.Define the inputs and outputs. The inputs required by this program are the values of the independent variables x and y.The output from the program will be the value of the function f(x,y). 3.Design the algorithm. Prompt the user for the values x and y. Readx andy if x>0 and y>0 funx+y elseif x>0 andy<0 im←-x+y^2 elseif x<0 andy>0 fium←-x2+y else fim←x2+y2 end Write out f,以 Turn the algorithm into MATLAB statements. The final MATLAB code follows. Script file:funxy.m Purpose: This program solves the function f(x,y)for a user-specified x and y,where f(x,y)is defined as: % x+y x>=0 and y>=0 % X+y^2 x>=0andy<0 % f(x,y)=x2+y x<0andy>=0 % x^2+y2 x<0 andy>0 % % Record of revisions: % Date Programmer Description of change 33 例 2.4.2 Evaluating a Function of Two Variables Write a MATLAB program to evaluate a function f(x.y) for any two user-specified values x and y. The function f(x,y) is defined as follows. The function f (x,y) is evaluated differently depending on the signs of the two independent variables x and y To determine the proper equation to apply, it will be necessary to check for the signs of the x and y values supplied by the user. 1. State the problem. Evaluate the function f(x,y) for any user-supplied values of x and y. 2. Define the inputs and outputs. The inputs required by this program are the values of the independent variables x and y. The output from the program will be the value of the function f(x,y). 3. Design the algorithm. Prompt the user for the values x and y. Read x and y if x > 0 and y > 0 fun← x + y elseif x > 0 and y < 0 fun← x + y^2 elseif x < 0 and y >_ 0 fun ← x^2 + y else fun ← x^2 + y^2 end Write out f(x,y) Turn the algorithm into MATLAB statements. % The final MATLAB code follows. % Script file: funxy.m % Purpose: % This program solves the function f(x,y) for a % user-specified x and y, where f(x,y) is defined as: % x + y x >= 0 and y >= 0 % x + y^2 x >= 0 and y < 0 % f (x, y)= x^2 + y x < 0 and y >= 0 % x^2 + y^2 x < 0 and y > 0 % % Record of revisions: % Date Programmer Description of change ⎪ ⎪ ⎩ ⎪ ⎪ ⎨ ⎧ + < < + < ≥ + ≥ < + ≥ ≥ = 0 0 0 0 0 0 0 0 ( , ) 2 2 2 2 x y x and y x y x and y x y x and y x y x and y f x y
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有