How to fix this. Please help!

//Question: Before interpolation the A matrix data i was unable to execute it because it was throwing error like A(2.6) for diffrent n(always integer) integers. Then i tried interpolating my matrix A . Its not giving proper results. Also i want my final answer to be displayed after for loop
t = 1 : 9;
t1= 1 : 0.1: 9;
A = [916.3 , 923.6 , 933.1, 947.4, 966.2, 986.6, 1008.5, 1031.5, 1051.3];
A1= interp1( t , A, t1 , 'nearest');
a=1;
b=9;
n=6;
h=(b-a)/n;
for i = 0:3
ans =0.5*[A1*(a+(i*h)) + A1(a+(i+1)*h)]*h;
end

 Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 6 Nov 2013
Edited: Azzi Abdelmalek on 6 Nov 2013
This line A1= interp1( t , A, t1 , 'nearest') is not correct, t is not defined, use
A1= interp1( time , A, t1 , 'nearest')
Add
a = 1;
b = 9;
n = 4;
h = (b-a)/n;
A = [916.3, 923.6, 933.1, 947.4, 966.2, 986.6, 1008.5, 1031.5, 1051.3 ];
for i = 0:3
res = 0.5 * [ A(a+(i*h)) + A(a+(i+1)*h) ]*h;
disp(res)
end

8 Comments

A(2.6) is not correct, the index should be a positive integer or logical.
If you want the value of A1 for t1=2.6,
A1(t1==2.6)
Rizwana
Rizwana on 6 Nov 2013
Edited: Rizwana on 6 Nov 2013
Hi: That was my typing mistake. Iam still getting not so correct value. Before interp1 function program is shown below: here the for loop executed 3 times and it displayed results because i have hand calculations it was matching. But this code was not working with variable n integers. Now iam not getting proper answer when i use interpolation
a = 1;
b = 9;
n = 4;
h = (b-a)/n;
A = [916.3, 923.6, 933.1, 947.4, 966.2, 986.6, 1008.5, 1031.5, 1051.3 ];
for i = 0:3
ans = 0.5 * [ A(a+(i*h)) + A(a+(i+1)*h) ]*h
disp('ans')
end
Thank You
a = 1;
b = 9;
n = 4;
h = (b-a)/n;
A = [916.3, 923.6, 933.1, 947.4, 966.2, 986.6, 1008.5, 1031.5, 1051.3 ];
for i = 0:3
res = 0.5 * [ A(a+(i*h)) + A(a+(i+1)*h) ]*h;
disp(res)
end
Rizwana
Rizwana on 6 Nov 2013
Edited: Rizwana on 6 Nov 2013
this wont work when i change n value to 5 or 6 or some other value also o/p is something like this:
res=
1.8494 e+003
1.8494 e+003
res=
1.8993e+003
1.8993e+003
res=
1.9747e+003
1.9747e+003
res=
2.059e+003
2.059e+003
// instead of multiple results i would like to have one res showing:
res=(1.8494 e+003)+(1.8993e+003)+(1.9747e+003)+(2.059e+003)
Thank You
%If a+(i*h)) and a+(i+1)*h represent time
a = 1;
b = 9;
n = 5;
h = (b-a)/n;
t = 1 : 9;
A = [916.3, 923.6, 933.1, 947.4, 966.2, 986.6, 1008.5, 1031.5, 1051.3 ];
for i = 0:3
res=0.5*h*(interp1(t,A,a+i*h)+interp1(t,A,a+(i+1)*h));
disp(res)
end
Here the matrix A represents presure quantities at time 1 to 9... i.e; A(1)=916.3---> presurre recorded at time 1sec A(2)=923.6---> presure recorded at time 2 sec... when i change n value to 5; then A in my program becomes A(1.6) This is throwing error... Hence i splitted the time array from 1:0.1:9 then interpolated the presuure data i.e ; A matrix to these values... Tried coding using program... Not getting answer with interpolated values... I tried to run your program.. Its throwing error: subscript indices must be real positive or logical... Thank You
I don't think you tried the correct program, copy ant paste the code in my previous comment. It works without errors
Thank You so much.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!