5 Compare a script and a function a Write a script In the ma
5) Compare a script and a function a) Write a script: In the main menu of Matlab, select file -> new - M-file A ne ew window will pop up. Input the following commands: x = 1:5; y = 6:10; g = x+y; and then save the file as myscript.m under the default path matlab/work b) Write a function: Create a new .m file following the procedure of above. Type in the commands: function g myfunction (x, y) g=x+y; and then save it as myfunction.m c) Compare their usage i) run the commands one by one myscript
Solution
(5)
(a)
x=1:5;
y=6:10;
g=x+y
OUTPUT:
g =
7 9 11 13 15
(b)
function [g]=myfunction(x,y)
g=x+y;
end
(C)
x
y
g
(ii)
clc;
clear all;
(iii)
x=1:5;
y=6:10;
z=myfunction(x,y)
Here, x and y variable passed through myfunction(x,y) and result value assign \"z\". If simply calls myfunction then shows error.
a=1:10;
b=2:11;
myfunction(a,b)
Here, x and y variable passed through myfunction(x,y) and result directly displayed in command window and ans variable. If simply calls myfunction then shows error.
