Evaluating a function with three variables

Dear all, Since I am new in MatLab, I want to evaluate the following equation y=(1./((x.^2)+1))-(p./((x+w).^2+1))-(p./((x-w).^2+1)); I want to sweep the variables x,p and w as follows x from -5 to 5 with 100 steps ,p from 0 to 1 with 100 steps ,w from 0 to 3 with 100 steps and then I want to normalized the functiony and then I want to extract the specific values of x (lets name new variable r) which give me specific value of y (lets say y=0.5) for each combination of variables x,w,p. After that I want to make a surface graph of ( r,w and p)
thanks

 采纳的回答

KSSV
KSSV 2018-9-26
编辑:KSSV 2018-9-26
N = 100 ;
x = linspace(-5,5,N) ;
p = linspace(0,1,N) ;
w = linspace(0,3,N) ;
[x,p,w] = meshgrid(x,p,w) ;
y=(1./((x.^2)+1))-(p./((x+w).^2+1))-(p./((x-w).^2+1));
figure
hold on
for i = 1:N
surf(x(:,:,i),p(:,:,i),w(:,:,i),y(:,:,i))
end
% GEt manually y = 0.5
idx = abs(y-0.5)<=10^-3 ;
xr = x(idx) ;
pr = p(idx) ;
wr = w(idx) ;
figure
scatter3(xr,pr,wr,10,wr)
% use isosurface
p = patch(isosurface(x,p,w,y,0.5));
p.FaceColor = 'red';
p.EdgeColor = 'none';
daspect([1 1 1])
view(3);
axis tight
camlight
lighting gouraud

6 个评论

thank you for answer.
now, I to want to extract values of x which give me certain values of y. lets say r= value of x which gives me the certainvalues of y for each combination, I define for example this required value of y as follows yabs=abs(min(y)); Ycertain=(max(y)-yabs)/2; and make a surface graph (r,w,p)
thanks
I have edited the answer...
kindly. I want now to not get the value of y as 0.5, but I want it as Ycertain=(max abs(y)-min(abs(y))/2 and then extract the value of corresponding x thanks
You find the value and and place the value inplace of 0.5....it is simple.
val = max(y(:)-abs(y(:)/2)) ;
I have made the modification and change the value of y to idx = (max(y(:))-(abs(min((y(:)))))/2) however, I got one value of idx and now values of xr and got the following message #Array indices must be positive integers or logical values#.
You would not use val as idx. You would use
idx = abs(y-val)<=10^-3 ;

请先登录,再进行评论。

更多回答(1 个)

I have made this modification to make surface and make xr as Z dimension , but it doesnot work properly
N = 100 ;
x = linspace(-5,5,N) ;
p = linspace(0,1,N) ;
w = linspace(0,3,N) ;
[x,p,w] = meshgrid(x,p,w) ;
y=(1./((x.^2)+1))-(p./((x+w).^2+1))-(p./((x-w).^2+1));
figure
hold on
for i = 1:N
surf(x(:,:,i),p(:,:,i),w(:,:,i),y(:,:,i))
end
% GEt manually y
val=(max(y(:))-(abs(min((y(:)))))/2)
idx = abs(y-val)>=10^-3 ;
xr = x(idx) ;
pr = p(idx) ;
wr = w(idx) ;
figure
scatter3(xr,pr,wr,10,wr)
% use isosurface
p = patch(isosurface(xr,pr,wr,y,val));
p.FaceColor = 'red';
p.EdgeColor = 'none';
daspect([1 1 1])
view(3);
axis tight
camlight
lighting gouraud

4 个评论

You cannot use isosurface() for scattered points.
idx = abs(y-val)>=10^-3 ;
is creating a logical mask that talks about individual points inside the grid y . When you take x(idx) and so on you are extracting only those points -- scattered points.
Consider
yt = y;
yt(~idx) = nan;
p = patch(isosurface(x, y, w, yt, val));
You will probably want to do that in a different plot, or erase what you have drawn first: the values form a thin irregular sheet that is effectively hidden by the heavy point density of the scatter3()
Thank you The bellow program returns a message The size of X must match the size of V or the number of columns of V. and show a plot of p as z axis which should be xr as z axis
N = 100 ;
x = linspace(-5,5,N) ;
p = linspace(0,1,N) ;
w = linspace(0,3,N) ;
[x,p,w] = meshgrid(x,p,w) ;
y=(1./((x.^2)+1))-(p./((x+w).^2+1))-(p./((x-w).^2+1));
figure
hold on
for i = 1:N
surf(x(:,:,i),p(:,:,i),w(:,:,i),y(:,:,i))
end
% GEt y
val=(max(y(:))-(abs(min((y(:)))))/2)
idx = abs(y-val)>=10^-3 ;
yt = y;
yt(~idx) = nan;
xr = x(idx) ;
pr = p(idx) ;
wr = w(idx) ;
figure
scatter3(xr,pr,wr,10,wr)
% use isosurface
p= patch(isosurface(xr, pr, wr, yt, val));
p.FaceColor = 'red';
p.EdgeColor = 'none';
daspect([1 1 1])
view(3);
axis tight
camlight
lighting gouraud
Notice in my call I did not select subsets of x and so on for the isosurface call: I left them the original size but assigned nan to locations in the grid that were not of interest.

请先登录,再进行评论。

类别

Community Treasure Hunt

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

Start Hunting!

Translated by