What is the problem for the following code?

clc; clear all; close all;
fprintf('\n************************************************************');
fprintf('\n*** Interpolation by Newtons Forward Difference Formula *'); fprintf('\n************************************************************'); n = input('\nEnter number of data points = '); h = input('\nEnter step size (h) = ') x(1) = input('\nx0 = '); y(1) = input('y0 = '); for i=2:n x(i)=x(i-1)+h; fprintf('\nX%d = %f',i,x(i)); fprintf('\t\tY%d: ',i); y(i) = input(''); end x_reqd = input('\nEnter X for which value of Y is sought: '); s=(x_reqd-x(1))/h; for i=1:n diff(i,1)=y(i); end %% Calculate Forward Differance Table for j=2:n for i=1:n-j+1 diff(i,j)=diff(i+1,j-1)-diff(i,j-1); end end fprintf('\n\t Forward Differance Table');
%% Print Forward Differance Table offset=1; for i=0:n*2-2 index=floor(i/2); fprintf("\t\t") if mod(i,2)==0 fprintf('\n %.6f',x(index+offset)); end if n>i j_max=i else j_max=n*2-i-1 end
for j=0:j_max
if mod(i,2)==mod(j,2)
fprintf('\t\t%.6f',diff(index-j/2+offset,j+offset));
end
end
end

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 11 月 27 日

0 投票

h = input('\nEnter step size (h) = ') x(1) = input('\nx0 = ');
You need a comma or a semi-colon between those two commands.
Better yet would be to format your code with one command per line. Whitespace is free, but the cost of a human trying to figure out what your run-on code does is not free.

5 件のコメント

PULAK Kumer
PULAK Kumer 2020 年 11 月 27 日
The program is in single line..bt this codes shows index number is invalid
Walter Roberson
Walter Roberson 2020 年 11 月 27 日
Yes, that can happen when you do not use line breaks or semi-colons in places you need them.
Fix the code to have only one command per line.
PULAK Kumer
PULAK Kumer 2020 年 11 月 27 日
clc;
clear all;
close all;
fprintf('\n************************************************************');
fprintf('\n*** Interpolation by Newtons Forward Difference Formula *');
fprintf('\n************************************************************');
n = input('\nEnter number of data points = '); h = input('\nEnter step size (h) = ');
x(1) = input('\nx0 = ');
y(1) = input('y0 = ');
for i=2:n
x(i)=x(i-1)+h;
fprintf('\nX%d = %f',i,x(i));
fprintf('\t\tY%d: ',i);
y(i) = input('');
end
x_reqd = input('\nEnter X for which value of Y is sought: ');
s=(x_reqd-x(1))/h;
for i=1:n
diff(i,1)=y(i);
end
%% Calculate Forward Differance Table
for j=2:n
for i=1:n-j+1
diff(i,j)=diff(i+1,j-1)-diff(i,j-1);
end
end
fprintf('\n\t Forward Differance Table');
%% Print Forward Differance Table
offset=1;
for i=0:n*2-2
index=floor(i/2);
fprintf("\t\t")
if mod(i,2)==0
fprintf('\n %.6f',x(index+offset));
end
if n>i
j_max=i;
else
j_max=n*2-i-1;
end
for j=0:j_max
if mod(i,2)==mod(j,2)
fprintf('\t\t%.6f',diff(index-j/2+offset,j+offset));
end
end
end
Show that :
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in kkkuntitled (line 43)
fprintf('\t\t%.6f',diff(index-j/2+offset,j+offset));
what can i do now?
Walter Roberson
Walter Roberson 2020 年 11 月 27 日
Change
fprintf('\t\t%.6f',diff(index-j/2+offset,j+offset));
to
fprintf('\t\t%.6f',diff(index-floor(j/2)+offset,j+offset));
Walter Roberson
Walter Roberson 2020 年 11 月 27 日
By the way, my belief is that the original C code had bugs.
Difficult to be sure, though, due to the lack of documentation... it is always possible that it had strange design requirements and was manually optimized to fit those requirements, and so only looks like it has mistake piled on mistake.

この質問は閉じられています。

タグ

質問済み:

2020 年 11 月 27 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by