linspace is causing an error
9 ビュー (過去 30 日間)
古いコメントを表示
My goal is to plot R, where R=1-T
T is a function of n2 and D, where n2 takes the values
n2 = [1.2 1.75 2.2] and D = linspace(0,1) and the function of T is equal to:
T =
So my code so far is *note(lambda = 1):
n2 = [1.2 1.75 2.2];
D = linspace(0,1);
T= (9.68.*n2.^2)./(11.6964.*n2.^2)+(1-n2.^2).*(5.8564-n2.^2).*(sin((2*pi*D)./1)).^2
>>Arrays have incompatible sizes for this operation.<<
I think what is happening is D which is 1x100 matrix is making this incompatible.
My end goal is to plot this and end up with 3 graphs, where one graph represents a value of n2 going through all of D.
I am very inexperienced at matlab but im trying to figure this out. Any help?
5 件のコメント
dpb
2022 年 10 月 12 日
NOTA BENE:
The equation for T used by both @Image Analyst and @VBBV is the same as that originally posted that has a transcription error in not including all the additive terms in the denominator.
See the later Answer I posted that corrects the equation-- the result is much different.
"There be dragons!"
採用された回答
dpb
2022 年 10 月 12 日
編集済み: dpb
2022 年 10 月 12 日
In
%T= (9.68.*n2.^2)./(11.6964.*n2.^2)+(1-n2.^2).*(5.8564-n2.^2).*(sin((2*pi*D)./1)).^2
you're missing a needed set of parentheses around the denominatior; as you've written it only the first term is in the denominator. You can get by by moving that first closing ")" to the end--
%T= (9.68*n2.^2)./(11.6964*n2.^2 + (1-n2.^2).*(5.8564-n2.^2).*(sin((2*pi*D))).^2);
For the other, you need to write for a single value of n2 and evaluate over D in a loop or investigate meshgrid to vectorize over both.
For the beginner, the looping construct first is probably the simpler approach.
ADDENDUM
But, the meshgrid solution is not that bad and for completeness since I did bring it up... :)
all_n = [1.2 1.75 2.2];
[N,D]=meshgrid(all_n(:),linspace(0,1).');
fnT=@(n2,D) 9.68*n2.^2./(11.6964*n2.^2+(1-n2.^2).*(5.8564-n2.^2).*(sin((2*pi*D)./1)).^2);
T=fnT(N,D);
plot(D,T)
grid on
xlabel('D'); ylabel('T')
caption = compose('n2 = %.2f', all_n.');
legend(caption,'location','north');
I would note again my first comment that the equation for T as written in the original posting is in error in that only the first term is incorporated in the denominator whereas the equation as presented in the image clearly has all additive terms in the denominator. This changes the numerical values greatly, although the general shape is similar, it is also flipped upside down from the incorrect version.
4 件のコメント
dpb
2022 年 10 月 13 日
編集済み: dpb
2022 年 10 月 14 日
Oh. Missed that (or forgot about it more accurately) since your code didn't compute R but stopped at T, so did all of us (I think).
That's trivial, of course, once you have T.
Or, you could redefine fnT to return R instead easily enough as well...
...
fnR=@(n2,D) 1-9.68*n2.^2./(11.6964*n2.^2+(1-n2.^2).*(5.8564-n2.^2).*(sin((2*pi*D)./1)).^2);
R=fnR(N,D);
plot(D,R)
grid on
xlabel('D'); ylabel('R')
caption = compose('n2 = %.2f', all_n.');
legend(caption,'location','north');
fnT=@(n2,D) 9.68*n2.^2./(11.6964*n2.^2+(1-n2.^2).*(5.8564-n2.^2).*(sin((2*pi*D)./1)).^2);
fnR=@(n2,D) 1-fnT(n2,D);
R=fnR(N,D);
...
for easier debugging (with or without his other suggestion of building the denominator piecewise).
その他の回答 (2 件)
VBBV
2022 年 10 月 12 日
n2 = [1.2 1.75 2.2];
D = linspace(0,1,100);
for k = 1:length(D)
T(:,k)= (9.68.*n2.^2)./(11.6964.*n2.^2)+(1-n2.^2).*(5.8564-n2.^2).*(sin((2*pi*D(k))./1)).^2;
end
plot(D,T)
0 件のコメント
Image Analyst
2022 年 10 月 12 日
It's because n2 and D are different sizes so you can't do it all in one equation. Try a loop
all_n = [1.2 1.75 2.2];
D = linspace(0,1);
whos all_n
whos D
for k = 1 : length(all_n)
% Get this one value of n
n2 = all_n(k);
% Compute T
T = (9.68.*n2.^2)./(11.6964.*n2.^2)+(1-n2.^2).*(5.8564-n2.^2).*(sin((2*pi*D)./1)).^2;
% Plot the curve
subplot(3, 1, k);
plot(D, T, 'b-', 'LineWidth', 2)
grid on
xlabel('D');
ylabel('T')
caption = sprintf('n2 = %.2f', n2);
title(caption);
fontsize(gca, 12, 'points')
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Filter Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!