正在加载图片...
12/05/98 S.J.Chapman Original code % Define variables: % x--First independent variable % y--Second independent variable % fun--Resulting function 的 Prompt the user for the values x and y x=input ('Enter the x coefficient:) y input ('Enter the y coefficient:) Calculate the function f(x,y)based upon the signs of x and y. ifx>=0&y>=0 fun=x+y; elseif x>=0&y<0 fun=x+y2; elseifx<0&y>=0 fun=x 2+y; else fun=x 2+y2; end Write the value of the function. disp ([The value of the function is'num2str(fun))); fprintf(The value of the function is %fn'fun): Test the program. >>funxy Enter the x coefficient:2 Enter the y coefficient:3 The value of the function is 5 >>funxy Enter the x coefficient:2 Enter the y coefficient:-3 The value of the function is 11 >>funxy Enter the x coefficient:-2 Enter the y coefficient:3 The value of the function is 7 >>funxy Enter the x coefficient:-2 Enter the y coefficient:-3 The value of the function is 13 ■2.4.3函数(Function) MATALB中许多常用的函数都是函数式M文件。函数被调用时,通过获取外部参数进行运算,并向 44 % 12/05/98 S. J. Chapman Original code % Define variables: % x -- First independent variable % y -- Second independent variable % fun -- Resulting function % Prompt the user for the values x and y x = input ('Enter the x coefficient:'); y = input ('Enter the y coefficient:'); % Calculate the function f(x,y) based upon % the signs of x and y. if x >= 0 & y >= 0 fun = x + y; elseif x >= 0 & y < 0 fun = x + y^2; elseif x < 0 & y >= 0 fun = x^2 + y; else fun = x^2 + y^2; end % Write the value of the function. % disp (['The value of the function is ' num2str(fun)]); fprintf(‘The value of the function is %f\n‘,fun); Test the program. >>funxy Enter the x coefficient: 2 Enter the y coefficient: 3 The value of the function is 5 >>funxy Enter the x coefficient: 2 Enter the y coefficient: -3 The value of the function is 11 >>funxy Enter the x coefficient: -2 Enter the y coefficient: 3 The value of the function is 7 >>funxy Enter the x coefficient: -2 Enter the y coefficient: -3 The value of the function is 13 ■ 2.4.3 函数(Function) MATALB 中许多常用的函数都是函数式 M 文件。函数被调用时,通过获取外部参数进行运算,并向
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有