如何在MATLAB中保存向量中的值并绘制它们?

时间:2016-05-03 13:05:13

标签: matlab

我在一小时内尝试在matlab中绘制图表,但卡在了以下内容中:

n1=1; %konstant
n2=1.5; %konstant

theta1=0:90; % I define my degree values from 0 to 90 
theta2=asind(n1*sind(theta1))/n2; % Snell's law : I calculate the other angle values which are depending on the values of the theta1

% NOW I WANT TO CALCULATE THE Rparallell VALUES WHICH ARE DEPENDING ON TWO INPUTS(theta1 and theta2) 

%Here is my first attempt to calculate the Rparallell : it does not work
%because the size of Rparallell is 1 by 1

for i=1:length(theta1)

    for k=1:length(theta2)
    Rparallell=(((n2*cosd(theta1)-(n1*cosd(theta2))))/((n2*cosd(theta1))+(n1*cosd(theta2)))).^2;
    end

end

%Here is my second attempt to calculate the Rparallell : it does not work
%because the size of Rparallell is 1 by 1

for i=1:91
    Rparallell=(((n2*cosd(theta1(:,i))-(n1*cosd(theta2(:,i)))))/((n2*cosd(theta1(:,i)))+(n1*cosd(theta2(:,i))))).^2;
end

当我执行代码时,我将Rparallell作为1对1向量,但这不是我想要的......我希望Rparallell成为1乘91值的向量..

我甚至试图插入Rparallell=zeros(1,91),但它没有帮助使Rparallell成为1乘91值的矢量。

我想在计算theta1的值及其相应的theta2值时计算Rparallell值。

如何更正代码,使Rparallell成为1乘91值的向量?

非常感谢

1 个答案:

答案 0 :(得分:2)

这应该可以帮到你

Rparallell=zeros(1,91)
for i=1:91
    Rparallell(i)=(((n2*cosd(theta1(:,i))-(n1*cosd(theta2(:,i)))))/((n2*cosd(theta1(:,i)))+(n1*cosd(theta2(:,i))))).^2;
end

Rparallell(i)访问Rparallell的第i个元素,因此每个值都存储在此向量的不同元素中。