i have a function and i need to find the output of the last value. y(n)=1/2*(​y(n-1)+A^2​/y(n-1)) 0<=n<=N-1 (N=30 A=4),When i run it says output arguments not assignet.

1 回表示 (過去 30 日間)
Tasos Apostolopoulos
Tasos Apostolopoulos 2022 年 1 月 25 日
編集済み: Harry 2022 年 1 月 25 日
function res = my_matlab_function(A,N)
y(1)=1/2*(3+(A^2/3));
y(2)=1/2*(y(1)+(A^2/y(1)));
for n=2:(N-1)
y(n)=1/2*(y(n-1)+A^2/y(n-1));
end

回答 (1 件)

Harry
Harry 2022 年 1 月 25 日
編集済み: Harry 2022 年 1 月 25 日
Hi Tasos,
you have not assigned y to res whihc is your function output. I corrected your code in the following way:
function res = my_matlab_function(A,N) % res is te function output
y(1)=1/2*(3+(A^2/3));
y(2)=1/2*(y(1)+(A^2/y(1)));
for n=2:(N-1)
y(n)=1/2*(y(n-1)+A^2/y(n-1));
end
res = y; % Assign it to the res
end
and then you can extract the last value by res(end). I hope it works for you.

カテゴリ

Help Center および File ExchangeDenoising and Compression についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by