MATLAB Answers

how do i print only the last answer in a for loop

2 views (last 30 days)
Zheng Xuan Lim
Zheng Xuan Lim on 14 Nov 2020
Answered: VBBV on 14 Nov 2020
function c = newton_interpolation(x,y)
[p,n]=size(x);
c = zeros(n, n);
c(:, 1)= y';
for j=2:n
for k=1:(n-j+1)
c(k,j) = (c(k+1, j-1) - c(k,j-1))/(x(k+j-1)-x(k))
end
end
when i print this i get every itteration of the answer but i only need the last

  0 Comments

Sign in to comment.

Answers (2)

madhan ravi
madhan ravi on 14 Nov 2020
c(k,j) = (c(k+1, j-1) - c(k,j-1))/(x(k+j-1)-x(k)); %<- semicolon

  0 Comments

Sign in to comment.


VBBV
VBBV on 14 Nov 2020
%if true
% code
%end
fprintf('The last answer of for loop is : %f',c(end,end))
Place this statement outside of the for loops.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by