作业帮 > 综合 > 作业

matlab loop

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/17 05:24:06
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.
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.
再问: 打进去了 虽然不对 但是我改了下 解决了 可以再帮我做一个吗
再答: 要做什么?这个我没编译,直接在板子上写的…… 错了点,不好意思啊