Mismatch between 'formula' and graphed 'surface'

1 回表示 (過去 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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by