linspace is causing an error
9 views (last 30 days)
Show older comments
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 Comments
dpb
on 12 Oct 2022
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!"
Accepted Answer
dpb
on 12 Oct 2022
Edited: dpb
on 12 Oct 2022
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 Comments
dpb
on 13 Oct 2022
Edited: dpb
on 14 Oct 2022
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).
More Answers (2)
VBBV
on 12 Oct 2022
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 Comments
Image Analyst
on 12 Oct 2022
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 Comments
See Also
Categories
Find more on Filter Analysis 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!