Mismatch between 'formula' and graphed 'surface'

1 view (last 30 days)
Hi guys,
I have a feeling that my code isn't generating the surface I want. I basically have this test function:
x = [0:50];
y = [0:50];
ModY = @(x,y) [20*(y<20) + 80*(y>80) + y*((y>=20)&(y<=80))];
Test1 = @(x,y) ((1300./ModY(x,y)-3.34)-(1.3./((1.3./(x))-((3.44)./998))));
[X1,Y1] = meshgrid(x,y);
Z1 = Test1(X1,Y1);
s1 = surf(X1,Y1,Z1,'LineStyle','none');
But when I test the function as follows:
Test1(40,20)
ans =
16.9144
I'm not convinced that it's a match. I feel like something is happening in my condensed code. Please help! Thank you!

Accepted Answer

Star Strider
Star Strider on 4 May 2015

I’m not convinced either:

The problem is here:

ModY = @(y) [20*(y<20) + 80*(y>80) + y.*((y>=20)&(y<=80))];
                                      ^  NEED ARRAY MULTIPLICATION HERE

You need to do array multiplication of ‘y’ and the logical array created by the ‘((y>=20)&(y<=80))’ inequality. (I recognised this because I’ve seen something like this code quite recently!)

With that, it works:

The reason it produced one value with the scalars and another with the matrices was due to the omitted element-wise array operator multiplication (.*).

  2 Comments
A
A on 4 May 2015
You are so good! Thank you so much again! I can't believe I made a thread about a '.'
Thanks again!
Star Strider
Star Strider on 4 May 2015
My pleasure! Thank you for the compliment!
I didn’t see anything wrong with your code to calculate ‘Test1’, so I plotted ‘ModY’ and the problem immediately showed itself.
The dot operator differentiates matrix from element-wise operations (except for the transpose operator, where its presence denotes the ‘normal’ transpose and its absence denotes the complex-conjugate transpose). For details on the differences, the discussion of Array vs. Matrix Operations is a page I’ve bookmarked.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!