Please solve the following question
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Adam Danz
le 3 Déc 2020
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.
- See GIF to learn how to set a break point: https://www.mathworks.com/matlabcentral/answers/552733-plotting-results-in-array-of-a-while-loop#comment_909634
- Matlab's page on Debugging: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
2 commentaires
Adam Danz
le 3 Déc 2020
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.
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!