Mismatch between 'formula' and graphed 'surface'

조회 수: 2 (최근 30일)
A
A 2015년 5월 3일
댓글: Star Strider 2015년 5월 4일
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!

채택된 답변

Star Strider
Star Strider 2015년 5월 4일

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
A
A 2015년 5월 4일
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 2015년 5월 4일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by