Prev: image to data conversion
Next: xpc shared interrupts
From: Lorenzo Guerrasio on 16 Feb 2010 05:28 "peter " <petzh912(a)student.liu.se> wrote in message <hldl2m$n2r$1(a)fred.mathworks.com>... > Thanks for your answer, when I tried your code it gives me someting like an exponential curve, also when I choose q=0 then you get division by zero. I have a vector with predefined values which I'm trying to rotate. sorry,it was oviously -q/m >> m=3; >> q=2; >> [x,z]=meshgrid(-100:100); >> ysup=-q/m+m.*sqrt(x.^2+z.^2); >> yinf=-q/m-m.*sqrt(x.^2+z.^2); >> surf(x,z,ysup) >> hold on >> surf(x,z,yinf) This is no exponentaial, it's just that you are computing the surface over a rectangular grid. Of course you can do everything with polar coordinates, then you could have the shape of two cones.You might try this t=-5:.1:5; [X,Y,Z] = cylinder(m.*y+q); surf(X,Y,Z,'linestyle','none') > My current solution is to calculate the distance from each matrix element to the center and then interpolate the appropriate value, for instance distance 20.5 then I make a linear interpolation between element 20 and 21 from my vector. > There should exist an easier way...? I still cant understand why you want to interpolate...can't you just make the grid thinner? PS -q/m + (x.^2+z.^2) is the (cartesian) distance from the center
From: Lorenzo Guerrasio on 16 Feb 2010 09:45 ok, I see now your original problem, http://www.mathworks.com/matlabcentral/newsreader/view_thread/270178 now I think I got what you meant:so,if both x and y points are in ascending order, cylinder and interp1 should be enough: have a look at this: x=rand(20,1)*10; x=sort(x); y=rand(20,1)*10; y=sort(y);%your data figure subplot(1,2,1),plot(x,y,'x-'),axis square%plot the data highs=0:.1:ceil(max(y));; xi=interp1(y,x,highs);%find the values of the radius at regular interval hold on,plot(xi,highs,'r--')%just check that this is a good interpolation [X,Y,Z] = cylinder(xi);%in X and Y the carthesian vaues of the radius, in Z the %normalized hight subplot(1,2,2),surf(X,Y,Z.*max(highs),'linestyle','none'),axis square%the surface figure%just to show that the line belongs to the surface surf(X,Y,Z.*max(highs),'linestyle','none'),axis square hold on plot3(x,zeros(size(x)),y,'kx-','linewidth',3) view(0,0) Hope I got what you needed. Cheers L "Lorenzo Guerrasio" <lorenzo.guerrasio(a)email.it> wrote in message <hldrvl$i0s$1(a)fred.mathworks.com>... > "peter " <petzh912(a)student.liu.se> wrote in message <hldl2m$n2r$1(a)fred.mathworks.com>... > > Thanks for your answer, when I tried your code it gives me someting like an exponential curve, also when I choose q=0 then you get division by zero. I have a vector with predefined values which I'm trying to rotate. > > sorry,it was oviously -q/m > > >> m=3; > >> q=2; > >> [x,z]=meshgrid(-100:100); > >> ysup=-q/m+m.*sqrt(x.^2+z.^2); > >> yinf=-q/m-m.*sqrt(x.^2+z.^2); > >> surf(x,z,ysup) > >> hold on > >> surf(x,z,yinf) > > This is no exponentaial, it's just that you are computing the surface over a rectangular grid. Of course you can do everything with polar coordinates, then you could have the shape of two cones.You might try this > > t=-5:.1:5; > [X,Y,Z] = cylinder(m.*y+q); > surf(X,Y,Z,'linestyle','none') > > > My current solution is to calculate the distance from each matrix element to the center and then interpolate the appropriate value, for instance distance 20.5 then I make a linear interpolation between element 20 and 21 from my vector. > > There should exist an easier way...? > > I still cant understand why you want to interpolate...can't you just make the grid thinner? > PS > -q/m + (x.^2+z.^2) is the (cartesian) distance from the center
From: peter on 17 Feb 2010 03:12 Looks awesome! Thanks However my data won't always be in a sorted order. For instance if I add y(10)=2, the resulting "volume" get the wrong shape. Is this preventable?
From: Lorenzo Guerrasio on 17 Feb 2010 10:28
"peter " <petzh912(a)student.liu.se> wrote in message <hlg8d5$h2u$1(a)fred.mathworks.com>... > Looks awesome! Thanks > However my data won't always be in a sorted order. For instance if I add y(10)=2, the resulting "volume" get the wrong shape. Is this preventable? Hi,glad it helped. So the idea was to give a hint for the simple case of a monotonous function. You could have just had a look to cylinder to see how it generates the matrix. Extending the problem to the more general case is not that complex. You could make use of the pol2cart function, that transfor cylindric coordinates (since what you want is basically a cilinder) in chartesian. Here you are, don't know if is the most efficient, but looks as it works to me. Hope this help. x=rand(20,1)*10; y=x+randn(20,1);%generate your (x,y) data datasort=sortrows([x,y],1);%just sort the absciss,so you can plot a line theta=0:pi/10:2*pi;% ro=repmat(datasort(:,1),1,length(theta));%for each theta you have the same value z=repmat(datasort(:,2),1,length(theta));%of ro(that is x) and z(that is y) thetarep=repmat(theta,size(ro,1),1); [X,Y,Z]=pol2cart(thetarep,ro,z);%inbuilt function figure, subplot(1,2,1),plot(datasort(:,1),datasort(:,2),'kx-'),axis square%the line subplot(1,2,2),surf(X,Y,Z,'linestyle','none','facealpha',.7);%the surface axis square figure%plot them toghether surf(X,Y,Z,'linestyle','none','facealpha',.4); hold on plot3(datasort(:,1),zeros(size(datasort,1),1),datasort(:,2),'kx-','linewidth',1.5) axis square |