matlab loop (a) Define a function handle for the function f(x) = x2.(b) Define the variable S to equal zero.(c) Use a three-line for loop to add each of the following to S in the order given:f(10),f(9),...,f(2),f(1).Don’t display anything.(d) After
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 07:20:50
matlab loop (a) Define a function handle for the function f(x) = x2.(b) Define the variable S to equal zero.(c) Use a three-line for loop to add each of the following to S in the order given:f(10),f(9),...,f(2),f(1).Don’t display anything.(d) After
matlab loop
(a) Define a function handle for the function f(x) = x2.
(b) Define the variable S to equal zero.
(c) Use a three-line for loop to add each of the following to S in the order given:f(10),
f(9),...,f(2),f(1).Don’t display anything.
(d) After the loop use disp to display the final value of S.
matlab loop (a) Define a function handle for the function f(x) = x2.(b) Define the variable S to equal zero.(c) Use a three-line for loop to add each of the following to S in the order given:f(10),f(9),...,f(2),f(1).Don’t display anything.(d) After
f = @(x)x.^2; %(a) function handler defined
S = 0; %(b) assign the initial value 0 to variable S
for i = 10:-11:1 % (c) initiate the loop that i equals 10, 9, ..., 1
S = f(i); % (c) loop body, calling function handler to retrieve value of S corresponds to each i
end % (c) Thrid line of for loop, close loop
disp(S); %(d) display S with function disp() as required.