필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
William david
William david 2023년 7월 27일
답변: C B 2023년 7월 27일
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.

답변 (2개)

Star Strider
Star Strider 2023년 7월 27일
Could it be because that is how you defined it?
xi = 0.01:0.01:0.2;
pe = linspace(1,1.5,100);
p = zeros(length(xi),length(pe))
It seems that ‘p’ is preallocated as a (30x100) matrix of zeros (preallocation is good programming practice), and you are onlly writing tto the first 20 rows and the first 10 columns of it.
.

C B
C B 2023년 7월 27일
Looking at pe(j), i think your J loop should run for length of pe, is this what you are expecting?
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)
pe = 1×100
1.0000 1.0051 1.0101 1.0152 1.0202 1.0253 1.0303 1.0354 1.0404 1.0455 1.0505 1.0556 1.0606 1.0657 1.0707 1.0758 1.0808 1.0859 1.0909 1.0960 1.1010 1.1061 1.1111 1.1162 1.1212 1.1263 1.1313 1.1364 1.1414 1.1465
p = zeros(length(xi),length(pe));
length(pe)
ans = 100
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,:))

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by