MATLAB编程实现方程选择的解 【例】

% Scripte 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
% |_

% 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 vlaue of the function is ' num2str(fun)]);
posted @ 2013-03-05 21:50  dreamsyeah  阅读(184)  评论(0编辑  收藏  举报