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.

2 views (last 30 days)
Tasos Apostolopoulos
Tasos Apostolopoulos on 25 Jan 2022
Edited: Harry on 25 Jan 2022
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

Answers (2)

Harry
Harry on 25 Jan 2022
Edited: Harry on 25 Jan 2022
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.

VBBV
VBBV on 25 Jan 2022
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(end); % Assign the last value to res variable
end
Assign the last value as above to your function output variable res

Tags

Community Treasure Hunt

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

Start Hunting!