为什么我的代码在第11列产生了0的值?

huangapple go评论75阅读模式
英文:

Why is my code producing a 0's at the 11th Column

问题

突然,p的第11列突然降为零,并在其后的列中保持不变。我看不出这样的原因,因为用于计算p的变量并没有随机变化。

清除所有; 清屏; 关闭所有图形;

tic;

z1 = 1;
z1 = 2;
z2 = 2 * z1;
z = [z1z2];
la1 = 1/3;
la2 = 1/3;
la = [la1la2];
z_ave = (z1 * la2 + z2 * la1) / (la1 + la2);
sigma = 0.75;
theta = sigma / (sigma-1);
pe = 1.68;
xi = 0.01:0.01:0.2;

pe = linspace(1,1.5,100);
p = zeros(length(xi),length(pe))
for i = 1:20
    for j = 1:10
        p(i,j) = (1 + xi(i) * pe(j)^theta)^(1/theta)
    end
end

plot(pe,p(1,:))

我期望的值应该保持在0.94以下,但似乎存在问题。

英文:

All of a sudden the 11th column of p is falling to zero and stays that way for the rest of the columns following it. I see no reason for this as the variables used to compute p do not change randomly.

clear all; clc; close all;


tic;

z1 = 1;
z1 = 2;
z2 = 2*z1;
z = [z1,z2];
la1 = 1/3;
la2 = 1/3;
la = [la1,la2];
z_ave = (z1*la2 + z2*la1)/(la1 + la2);
sigma = 0.75; 
theta = sigma/(sigma-1);
pe = 1.68;
xi = 0.01:0.01:0.2;

pe = linspace(1,1.5,100);
p = zeros(length(xi),length(pe))
for i = 1:20
    for j = 1:10
p(i,j) = (1+xi(i)*pe(j)^theta)^(1/theta)
end
end

plot(pe,p(1,:))

I expect the values to stay around 0.94 and lower, but there seems to be a problem.

答案1

得分: 2

你在for循环中有一个错误。
使用1:length(pe)代替1:10

清除所有变量; 清除命令窗口; 关闭所有图形窗口;


计时开始;

z1 = 1;
z1 = 2;
z2 = 2*z1;
z = [z1,z2];
la1 = 1/3;
la2 = 1/3;
la = [la1,la2];
z_ave = (z1*la2 + z2*la1)/(la1 + la2);
sigma = 0.75; 
theta = sigma/(sigma-1);
pe = 1.68;
xi = 0.01:0.01:0.2;

pe = linspace(1,1.5,100);
p = zeros(length(xi),length(pe));
for i = 1:length(xi)
    for j = 1:length(pe)
        p(i,j) = (1+xi(i)*pe(j)^theta)^(1/theta);
    end
end

绘制(pe,p(1,:))
英文:

You had a mistake in the for loops.
Use 1:length(pe) instead of 1:10

clear all; clc; close all;


tic;

z1 = 1;
z1 = 2;
z2 = 2*z1;
z = [z1,z2];
la1 = 1/3;
la2 = 1/3;
la = [la1,la2];
z_ave = (z1*la2 + z2*la1)/(la1 + la2);
sigma = 0.75; 
theta = sigma/(sigma-1);
pe = 1.68;
xi = 0.01:0.01:0.2;

pe = linspace(1,1.5,100);
p = zeros(length(xi),length(pe));
for i = 1:length(xi)
    for j = 1:length(pe)
        p(i,j) = (1+xi(i)*pe(j)^theta)^(1/theta);
    end
end

plot(pe,p(1,:))

huangapple
  • 本文由 发表于 2023年7月27日 16:55:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定