# how to plot a surface in MATLAB?

2 views (last 30 days)
safi58 on 3 Feb 2017
Commented: Walter Roberson on 3 Feb 2017
lamda=0.218;
rl=2.2;
N=20
[fi, fn] = meshgrid(linspace(10,180,N),linspace(1,2,N));
gamma=pi./fn;
x=1-cosd(fi);
a=1-cosd(fi);
b=(gamma/rl)*(1-0.5*(1-cosd(fi)));
c=(0.25*lamda*(fi.*pi/180).^2)*(1-cosd(fi));
d=0.5*lamda*(fi.*pi/180).^2;
e=0.5*(fi.*pi/180).^2*lamda;
f=0.5*(fi.*pi/180)*lamda*sind(fi);
g=(0.5*(fi.*pi/180).^2*lamda)*(1-cosd(fi));
h=(0.25*(fi.*pi/180)*lamda*gamma)*(1-cosd(fi));
den=a+b-c+d-e+f-h+g;
Gdc=x./den
surf(fi, fn, Gdc, 'edgecolor', 'b')
I have to plot a surface by these equations but it is not giving me what i expected. Can anyone help me?
##### 2 CommentsShowHide 1 older comment
safi58 on 3 Feb 2017
Hi, Jon. No, I would not expect it. I would expect like this

Walter Roberson on 3 Feb 2017
You did not give us any ideas what you were expecting so it is difficult to debug.
Possibly in c, g, h, you want .* (1-cosd(fi)) instead of * (1-cosd(fi))
By the way, for efficiency you should calculate (1-cosd(fi)) only once and use the result multiple times.
Walter Roberson on 3 Feb 2017
I was hoping for mathematical equations, to reduce the ambiguity of .* compared to * as you coded some multiplications with * (algebraic matrix multiplication).
What you just posted contains in part
+(0.5*lamda*(fi.*pi/180).^2)-(0.5*(fi.*pi/180).^2*lamda)
As lamda is a scalar rather than a matrix, the lamda can be moved in front in both subexpressions,
+(0.5*lamda*(fi.*pi/180).^2)-(0.5*lamda*(fi.*pi/180).^2)
and you can see that the two sub-expressions are the same but of opposite signs and so will cancel out to 0. In your original question these were d and e
In the below code I have left them in as expressions d and e even though they mathematically cancel out, so as to make it easier for you to find the place that will have to be changed.
N = 50;
fi = linspace(0, 180, N) * pi/180;
fn = linspace(0.1, 5, N);
[FI, FN] = ndgrid(fi, fn);
lamda= 0.218;
rl = 2.2;
GAMMA = pi ./ FN;
cosFI = cos(FI);
M1cosFI = 1 - cosFI;
FIlamda = lamda .* FI;
FI2lamda = FIlamda .* FI;
d = (0.5*FI2lamda);
e = (0.5*FI2lamda);
Gdc = M1cosFI ./ (M1cosFI + ((GAMMA/rl) .* (1-0.5.*M1cosFI)) - ((0.25*FI2lamda).*M1cosFI) + d - e + (0.5*FIlamda.*sin(FI)) - ((0.25*FIlamda.*GAMMA).*M1cosFI) + (0.5*FI2lamda).*M1cosFI);
surf(FI, FN, Gdc)
This implements the equations fairly efficiently, but the outcome is not like you had hoped, which is partly due to the problem with d and e. But only partly -- over those ranges, these equations have a bunch of narrow peaks that would be difficult to miss if you happened to plot at the wrong resolution or with slightly the wrong locations.