Mismatch between 'formula' and graphed 'surface'

1 Ansicht (letzte 30 Tage)
A
A am 3 Mai 2015
Kommentiert: Star Strider am 4 Mai 2015
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!

Akzeptierte Antwort

Star Strider
Star Strider am 4 Mai 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 Kommentare
A
A am 4 Mai 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 am 4 Mai 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by