Invalid use of operator while trying to plot surface

2 views (last 30 days)
I am a total rookie when it comes to MatLab so I got stuck right away when trying to plot a surface. I get the "invalid use of operator" error code when trying to write the expression for Z, which is supposed to look something like this: z=(4x^2+8xy-2y^2)e^{-(x^2+2y^2)}. MatLab seems to point out that the minus sign in front of the 2 is the issue. How should I code the expression?
>> x=linspace(-3,3);
y=linspace(-2,2);
[X,Y]=meshgrid(x,y);
Z=(4*X.^2+8*X.*Y.-2*Y.^2)*exp^-(X.^2+2Y.^2);
Invalid use of operator.
Thanks in advance!

Accepted Answer

Dave B
Dave B on 20 Nov 2021
Edited: Dave B on 20 Nov 2021
A few issues:
  • There's no .- in MATLAB, you use the dot to indicate you want a element-wise operation instead of a matrix operation, but for + and - there's only one option.
  • exp is a function, so you can do exp(1)^n if you really want, but it's easier to just do exp(n)
  • Inside your exp power you have 2Y, but you meant 2*Y.
x=linspace(-3,3);
y=linspace(-2,2);
[X,Y]=meshgrid(x,y);
Z=(4*X.^2+8*X.*Y-2*Y.^2)*exp(-X.^2+2*Y.^2)
Z = 100×100
1.0e+05 * 0.0007 0.0010 0.0014 0.0019 0.0027 0.0038 0.0053 0.0072 0.0098 0.0133 0.0178 0.0237 0.0313 0.0411 0.0535 0.0692 0.0889 0.1132 0.1433 0.1799 0.2242 0.2775 0.3409 0.4156 0.5031 0.6045 0.7211 0.8538 1.0036 1.1710 0.0007 0.0010 0.0014 0.0020 0.0028 0.0039 0.0053 0.0073 0.0099 0.0134 0.0180 0.0240 0.0317 0.0416 0.0542 0.0702 0.0901 0.1148 0.1452 0.1823 0.2273 0.2812 0.3454 0.4212 0.5099 0.6127 0.7308 0.8653 1.0171 1.1868 0.0007 0.0010 0.0014 0.0020 0.0028 0.0039 0.0054 0.0074 0.0101 0.0136 0.0183 0.0243 0.0321 0.0422 0.0550 0.0711 0.0912 0.1163 0.1471 0.1847 0.2302 0.2849 0.3499 0.4267 0.5165 0.6207 0.7403 0.8766 1.0304 1.2022 0.0007 0.0010 0.0014 0.0020 0.0028 0.0040 0.0055 0.0075 0.0102 0.0138 0.0185 0.0246 0.0325 0.0427 0.0556 0.0720 0.0924 0.1177 0.1489 0.1870 0.2331 0.2885 0.3544 0.4321 0.5230 0.6285 0.7496 0.8876 1.0433 1.2174 0.0007 0.0010 0.0015 0.0021 0.0029 0.0040 0.0055 0.0076 0.0103 0.0139 0.0187 0.0249 0.0329 0.0432 0.0563 0.0728 0.0935 0.1192 0.1507 0.1893 0.2360 0.2920 0.3587 0.4373 0.5294 0.6361 0.7588 0.8984 1.0560 1.2322 0.0007 0.0010 0.0015 0.0021 0.0029 0.0040 0.0056 0.0077 0.0104 0.0141 0.0189 0.0252 0.0333 0.0437 0.0570 0.0737 0.0946 0.1206 0.1525 0.1915 0.2387 0.2954 0.3629 0.4425 0.5356 0.6436 0.7677 0.9090 1.0685 1.2467 0.0007 0.0010 0.0015 0.0021 0.0029 0.0041 0.0057 0.0078 0.0106 0.0143 0.0191 0.0255 0.0337 0.0442 0.0576 0.0745 0.0957 0.1219 0.1542 0.1937 0.2414 0.2988 0.3670 0.4475 0.5417 0.6509 0.7764 0.9193 1.0806 1.2608 0.0007 0.0011 0.0015 0.0021 0.0030 0.0041 0.0057 0.0078 0.0107 0.0144 0.0194 0.0258 0.0341 0.0447 0.0583 0.0754 0.0967 0.1233 0.1559 0.1958 0.2441 0.3021 0.3710 0.4524 0.5477 0.6581 0.7849 0.9294 1.0925 1.2747 0.0007 0.0011 0.0015 0.0021 0.0030 0.0042 0.0058 0.0079 0.0108 0.0146 0.0196 0.0261 0.0344 0.0452 0.0589 0.0762 0.0978 0.1246 0.1576 0.1979 0.2467 0.3053 0.3750 0.4572 0.5535 0.6651 0.7933 0.9393 1.1041 1.2882 0.0008 0.0011 0.0015 0.0022 0.0030 0.0042 0.0058 0.0080 0.0109 0.0147 0.0198 0.0263 0.0348 0.0457 0.0595 0.0769 0.0988 0.1259 0.1592 0.1999 0.2492 0.3084 0.3788 0.4619 0.5591 0.6719 0.8014 0.9489 1.1154 1.3014
Pro tip: break things up into small pieces...it's much easier to debug (and catch if you've not made the order of operations you intended!). For example:
a=4*X.^2;
b=8*X.*Y;
c=2*Y.^2;
d=-X.^2;
e=2*Y.^2;
Z2=(a+b-c)*exp(d+e);
ans = logical
1
  4 Comments
August Lindberg
August Lindberg on 20 Nov 2021
Never mind. I found my mistake: e should also be negative. Thanks for your help!
RITWIK SINGH
RITWIK SINGH on 9 Dec 2021
i have a similar issue in this line
EbN0Lin = 10.^(EbN0dB/10);
theoryBer_nRx1 = 0.5.(1-1(1+1./EbN0Lin).^(-0.5));
p = 1/2 - 1/2.(1+1./EbN0Lin).^(-1/2);
theoryBerMRC_Rx2 = p.^2.(1+2(1-p));
any idea how to fix?

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!