Matlab计算与仿真 Matlab编程(续) 函数的输入输出 twosum m two inputs, no output threesum. m- three inputs, one output addmult. m -two inputs, two outputs 1: function twosum(x,y 2: twosum Add two matrices 3: and print the result X+ 中山火學
Matlab计算与仿真 Matlab编程 (续) - 函数的输入输出 twosum.m — two inputs, no output threesum.m — three inputs, one output addmult.m — two inputs, two outputs ---------------------------------------------- 1: function twosum(x,y) 2: % twosum Add two matrices 3: % and print the result 4: x+y
Matlab计算与仿真 Matlab编程(续) function s= threesum(x, y, z) 2: threesum Add three variables 3: and return the result 4:5=X+y+z; 1: function [s, P]=addmult(x,y) 2:% addmult Compute sum and product 3: of two matrices 4:s= 5 p=×"y 中山火學
Matlab计算与仿真 Matlab编程 (续) 1: function s = threesum(x,y,z) 2: % threesum Add three variables 3: % and return the result 4: s = x+y+z; --------------------------------------------- 1: function [s,p] = addmult(x,y) 2: % addmult Compute sum and product 3: % of two matrices 4: s = x+y; 5: p = x*y;
Matlab计算与仿真 Matlab编程(续) fosun的结果 >>twosum(2, 2) >A=[12:34] ans B=[56:78] > twosum(A, B) X=[12y=[34]:ans= > twosum(xy) 68 ans 1012 46 nosum(one,two)%将字符串转换成整数组 ans 227229212 中山火學
Matlab计算与仿真 Matlab编程 (续) twosum的结果 >> twosum(2,2) ans = 4 >> x = [1 2]; y = [3 4]; >> twosum(x,y) ans = 4 6 >> A = [1 2; 3 4]; >> B = [5 6; 7 8]; >> twosum(A,B); ans = 6 8 10 12 >> twosum(’one’,’two’) % 将字符串转换成整数组 ans = 227 229 212
Matlab计算与仿真 Matlab编程(续) fosun的结果(续) >> clear >>disp(Ix y) ×X=4y=-2 4-2 >>twosum(1,2)>>who ans your variables are ans X >×+y ans %注意Xy没有改变 中山火學
Matlab计算与仿真 twosum的结果(续) >> clear >> x = 4; y = -2; >> twosum(1,2) ans = 3 >> x+y ans = 2 Matlab编程 (续) >> disp([x y]) 4 -2 >> who Your variables are: ans x y % 注意: x,y 没有改变
Matlab计算与仿真 Matlab编程(续) threes运行结果 >>a= threesum(1, 2, 3) a 6 > threesum(4, 5,6) ans 15 > b= threesum (7, 8, 9) 注:b的值为24 中山火學
Matlab计算与仿真 - threesum运行结果 >> a = threesum(1,2,3) a = 6 >> threesum(4,5,6) ans = 15 >> b= threesum(7,8,9); 注:b的值为24 Matlab编程 (续)