linspace is causing an error

9 visualizaciones (últimos 30 días)
Snow
Snow el 12 de Oct. de 2022
Editada: dpb el 14 de Oct. de 2022
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 comentarios
Snow
Snow el 12 de Oct. de 2022
Yes, I was looking for something like @VBBV's answer.
dpb
dpb el 12 de Oct. de 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!"

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 12 de Oct. de 2022
Editada: dpb el 12 de Oct. de 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 comentarios
Snow
Snow el 13 de Oct. de 2022
Editada: Snow el 13 de Oct. de 2022
@Image Analyst Thank you, that is a good way of going forway with coding.
@dpb I have been meaning to come back. I do believe the answer I accepted is incorect, more so after reading your code and trying to analyze it. I am not familiar with meshgrid so I will need to read up on that.
I am a bit confused though. Because overall I am suppose to be graphing R which R = 1-T. I personally dont see it in your code but maybe I am misunderstanding how your code works?
Edit: I should have been a little more clear, the question states:
"plot R as a function of D∈[0,1] for each of the 3 values of n2"
dpb
dpb el 13 de Oct. de 2022
Editada: dpb el 14 de Oct. de 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');
or, in the spirit of @Image Analyst, keep fnT as it was and add fnR as
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).

Iniciar sesión para comentar.

Más respuestas (2)

VBBV
VBBV el 12 de Oct. de 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)

Image Analyst
Image Analyst el 12 de Oct. de 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
Name Size Bytes Class Attributes all_n 1x3 24 double
whos D
Name Size Bytes Class Attributes D 1x100 800 double
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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by