How Can I Change the Function Interp2? I Wrote The Following Code But The Data Is Not Getting Fetched.What To Do For The Same?

r=0:1:7;
theta=0:2:4;
scan_lines=[1 1 2 3 4 5 6 7;2 3 4 5 6 7 8 9;3 4 5 6 7 8 9 10];
M=length(r);
N=length(theta);
P=0:.5:7;
Q=0:1:4;
[r_cart,theta_cart]=meshgrid(P,Q);
% K{i,j} ={r_cart,theta_cart};
m=length(P);
n=length(Q);
% Vq=0;
for i=1:1:N-1
del_theta=theta(i+1)-theta(i);
for j=1:1:M-1
del_r=r(j+1)-r(j);
for x=1:1:n
for y=1:1:m
delta=1/(del_theta*del_r);
a=(theta_cart(x)-theta(i))*(r_cart(y)-r(j));
b=(theta_cart(x)-theta(i))*(r(j+1)-r_cart(y));
c=(theta(i+1)-theta_cart(x))*(r_cart(y)-r(j));
d=(theta(i+1)-theta_cart(x))*(r(j+1)-r_cart(y));
end
end
end
end
% V=interp2(r,theta,scan_lines,r_cart,theta_cart);
for i=1:1:n
for j=1:1:m
Vq=delta*((a.*x(i+1,j+1))+(b.*x(i+1,j))+(c.*x(i,j+1))+(d.*x(i,j)));
end
end

1 Comment

r=0:1:7;
theta=0:2:4;
scan_lines=[1 1 2 3 4 5 6 7;2 3 4 5 6 7 8 9;3 4 5 6 7 8 9 10];
M=length(r);
N=length(theta);
P=0:.5:7;
Q=0:1:4;
[r_cart,theta_cart]=meshgrid(P,Q);
% K{i,j} ={r_cart,theta_cart};
m=length(P);
n=length(Q);
% Vq=0;
for i=1:1:N-1
del_theta=theta(i+1)-theta(i);
for j=1:1:M-1
del_r=r(j+1)-r(j);
for x=1:1:n
for y=1:1:m
delta=1/(del_theta*del_r);
a=(theta_cart(x)-theta(i))*(r_cart(y)-r(j));
b=(theta_cart(x)-theta(i))*(r(j+1)-r_cart(y));
c=(theta(i+1)-theta_cart(x))*(r_cart(y)-r(j));
d=(theta(i+1)-theta_cart(x))*(r(j+1)-r_cart(y));
end
end
end
end
% V=interp2(r,theta,scan_lines,r_cart,theta_cart);
for i=1:1:n
for j=1:1:m
Vq=delta*((a.*x(i+1,j+1))+(b.*x(i+1,j))+(c.*x(i,j+1))+(d.*x(i,j)));
end
end
i thought to use the above code instead of interp2,but the data fetching processing is not done in this code,so please help me to do the same

Sign in to comment.

Answers (2)

You can use griddedInterpolant:
sl = [1 1:7;2:9;3:10];
r = 0:7;
theta = 0:2:4;
[x,y] = ndgrid(theta,r);
F = griddedInterpolant(x,y,sl)
P = 0:.5:7;
Q = 0:.1:4;
[Qm,Pm] = ndgrid(Q,P);
V = F(Qm,Pm);
or used old function griddata:
sl = [1 1:7;2:9;3:10];
r = 0:7;
theta = 0:2:4;
[x,y] = ndgrid(theta,r);
P = 0:.5:7;
Q = 0:.1:4;
[Qm,Pm] = ndgrid(Q,P);
V = griddata(x,y,sl,Qm,Pm);
You are overwriting all of "a", "b", "c", and "d" in every iteration of your inner-most loop.
As your overwriting is inside a loop nested four down, "i", "j", "x", "y", in order to extract the data for use outside the loops, you would need to store to a(i,j,x,y), b(i,j,x,y) and so on.
You would then run into the difficulty that your Vq is overwritten in each iteration of the second set of nested loops. As you are accessing all of "a" and "b" and so on there, and we have determined that those would have to be four-dimensional matrices, then each Vq entry would have to be a four-dimensional matrix. As that is within two nested loops, you would therefore need to be constructing a six-dimensional matrix answer:
Vq(:,:,:,:,i,j) = ....
What you are going to do with a six-dimensional result is something I am sure I do not understand.
Perhaps the structure of your code is in error rather than the syntax.

Categories

Asked:

on 17 Dec 2013

Answered:

on 17 Dec 2013

Community Treasure Hunt

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

Start Hunting!