5) Compare a script and a function a) Write a script: In the main menu of Matlab
ID: 1993516 • Letter: 5
Question
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 myscriptExplanation / Answer
(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.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.