Please solve the following question

조회 수: 1 (최근 30일)
PULAK Kumer
PULAK Kumer 2020년 12월 3일
댓글: Adam Danz 2020년 12월 3일
clc;
clear all;
fprintf('*************************************************************************************************');
fprintf("\n************** Gauss's Forward Interpolation Formula ************************** *");
fprintf('\n****************In this code , y is dependent of x --> y=f(x) ***************************');
fprintf('\n****************<strong>Establishing Matlab Code By Pulak Kundu(Group 1)</strong>***************************')
fprintf('\n*************************************************************************************************');
n = input("\nHow many pair's of data do you want to input = ");
h = input('\nEnter the interval for independent variable which will equal for all time (h) = ');
x(1) = input('\nPlease input the value for x[0] = ');
y(1) = input('Please input the value for y[0] = ');
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
fprintf("\nEnter x-value for which value y=f(x) is calculated beteween %.4f to %.4f :",x(1),x(n))
xi = input('');
if xi<x(1) | xi>x(n)
fprintf("\t\tYou have pressed wrong value for interpolation.\n")
fprintf("\t\tThank you for using this code and Run the code again.")
return;
end
%The value of y will be stored in diff (differnt row, column1)
for i=1:n
diff(i,1)=y(i);
end
for j=2:n
for i=1:n-j+1
diff(i,j)=diff(i+1,j-1)-diff(i,j-1);
end
end
for i=1:n
if((xi>=x(i))&&(xi<=x(i+1)))
u=(xi-x(i))/h;
f=i;
end
end
result1=diff(f,1);
k=1;
j=1;
for i=1:n-1
if(i==1)
r=u;
result1=result1+r*(diff(f,i+1));
elseif((rem(i,2)==0)&&(i~=1))
r=r*(u-j)/i;
result1=result1+r*diff(f-floor(i/2),i+1);
j=j+1;
elseif((rem(i,2)==1)&&(i~=1))
r=r*(u+k)/i;
result1=result1+r*(diff(f-floor((i-1)/2),i+1));
k=k+1;
end
end
fprintf("\n\nThus ,Using Newton's General Interpolation Formula we get,")
fprintf("\n\t\tThe value of<strong> y= f(%.4f)</strong> will be =<strong> %.10f</strong>",xi,result1);
fprintf("\n\n*************Thank you very much for using this MATLAB Code From <strong> Pulak(181201)</strong> Group 1*************");
Question : If I run the previous code and and we put first value of x=0 and h=5 ,then x_read=3 then the following messege has been shown:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
result1=result1+r*diff(f-floor(i/2),i+1);---> for this line
what can i do now?
  댓글 수: 2
Adam Danz
Adam Danz 2020년 12월 3일
What inputs are you using for the 4 input questions?
PULAK Kumer
PULAK Kumer 2020년 12월 3일
n=4 h=10 x1=0 y1=22 y2=29 y3=31.5 y4=40 xi=1.25

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2020년 12월 3일
The variable diff is a 4x4 matrix.
diff =
22 7 -4.5 10.5
29 2.5 6 0
31.5 8.5 0 0
40 0 0 0
In this line below, on the first iteration f=1 and floor(i/2)=1 so f-floor(i/2) equals 0.
That's a problem because indexing requries non-zero positive integers. There is no zero-th row of a matrix in Matlab.
for i=1:n-1
...
result1=result1+r*diff(f-floor(i/2),i+1);
...
end
So you'll need to figure out what the code is supposed to be doing there and what went wrong. Your life will be easier if you use Matlab's debugging tools. Put a break point on that line, run the code, and it will pause on that line. You can see variable values and that will help you determine what's wrong.
  댓글 수: 2
PULAK Kumer
PULAK Kumer 2020년 12월 3일
What should i do now for fix this problem sir? I want to find interpolation value using Gauss's forward interpolation
Adam Danz
Adam Danz 2020년 12월 3일
You should learn to use debug mode and then step through your code to understand where it's going wrong.
If you want someone else to do that work for you they will first have to study the code, study the methods you want to implement, understand your approach in the code, and then find where that approach is going wrong. That could take all day.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by