Undefined function or variable 'p'. Error in test1 (line 32) px(j)=p(21,j);

2 ビュー (過去 30 日間)
n=41; h=1/(n-1); nt=360; tau=0.001;
for i=1:n
x(i)=(i-1)*h;
y(i)=(i-1)*h;
end
for j=1:n
for i=1:n
p0(i,j)=1;
end
end
tic
for jt=1:nt
t(jt)=jt*tau;
for j=1:n % Anlitik yechimni hisoblash
for i=1:n
pt(i,j)=exp(-2*x(i)*y(j)*t(jt));
end
end
end
for jt=1:nt/2 % Vaqt bo`yicha tsikl}
fxy(n,tau,h,p0,x,y,t,jt);
fyx(n,tau,h,p0,x,y,t,jt);
end % jt
toc
Elapsed time is 0.458155 seconds.
for j=1:n
px(j)=p(21,j);
pxt(j)=pt(21,j);
end
Unrecognized function or variable 'p'.
x=0:h:1;
y=0:h:1;
figure ('Position',[600 160 700 600]); %Natijalarni figuraga chiqarish oynasi: 750-eni, 650-boyi, 700-chapdan masofa, 75-yuqoridan masofa
subplot(3,2,1);
meshc(x,y,pt) %Uch o'lchovli 3D grafika
title('АНИК ЕЧИМ 3D ГРАФИГИ')
subplot(3,2,2);
meshc(x,y,p) %Uch o'lchovli 3D grafika
title('CОНЛИ ЕЧИМ 3D ГРАФИГИ')
subplot(3,2,3);
contour(x,y,pt,'ShowText','on','LineWidth',2); %kontur chizish
title('АНИК ЕЧИМ КОНТУР ГРАФИГИ')
subplot(3,2,4);
contour(x,y,p,'ShowText','on','LineWidth',2); %kontur chizish
title('CОНЛИ ЕЧИМ КОНТУР ГРАФИГИ')
subplot(3,2,5);
plot(x,px,x,pxt); %Ikki o'lchovli grafika
title('КЕСИМДА АНИК ВА CОНЛИ ЕЧИМ ЎЗГАРИШИ X БЎЙИЧА')
function [p0,p] = fxy(n,tau,h,p0,x,y,t,jt)
% k+0.5 vaqt qatlamida hisoblash
for j=2:n-1 % Tenglamalar koeffitsientini hisoblash
for i=2:n-1
%chekli ayirma tenglama koeffitsentlarini hisoblash
a(i)=1;
c(i)=1;
b(i)=2+2*h*h/tau;
ff=h*h*(-2*(2*t(jt)*t(jt)*(x(i)*x(i)+y(j)*y(j))+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d(i)=2*h*h/tau*p0(i,j)+(p0(i,j-1)-2*p0(i,j)+p0(i,j+1))+ff;
end
%Pragonka koeffitsentlarini hisoblash
aa1(1)=0; bb1(1)=1;
for i=2:n-1
aa1(i)=c(i)/(b(i)-aa1(i-1)*a(i));
bb1(i)=(d(i)+bb1(i-1)*a(i))/(b(i)-aa1(i-1)*a(i));
end
% bosim funktsiyasini hisoblash
p(n,j)=exp(-2*y(j)*t(jt));
for i=n-1:-1:1
p(i,j)=aa1(i)*p(i+1,j)+bb1(i);
end
end %j
for i=1:n % Chegaraviy qiymatlarni hisoblash
p(i,n)=(4.0*p(i,n-1)-p(i,n-2))/3;
p(i,1)=(4.0*p(i,2)-p(i,3))/3;
end
for j=1:n % k+1 qatlam uchun boshlangich qiymatni hisoblash
for i=1:n
p0(i,j)=p(i,j);
end
end
% k+1 vaqt qatlamida hisoblash
for i=2:n-1 % Tenglamalar koeffitsientini hisoblash
for j=2:n-1
%chekli ayirma tenglama koeffitsentlarini hisoblash
a(j)=1;
c(j)=1;
b(j)=2+2*h*h/tau;
ff=h*h*(-2*(2*t(jt)*t(jt)*(x(i)*x(i)+y(j)*y(j))+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d(j)=2*h*h/tau*p0(i,j)+(p0(i-1,j)-2*p0(i,j)+p0(i+1,j))+ff;
end
%Pragonka koeffitsentlarini hisoblash
aa1(1)=0; bb1(1)=1;
for j=2:n-1
aa1(j)=c(j)/(b(j)-aa1(j-1)*a(j));
bb1(j)=(d(j)+bb1(j-1)*a(j))/(b(j)-aa1(j-1)*a(j));
end
% bosim funktsiyasini hisoblash
p(i,n)=exp(-2*x(i)*t(jt));
for j=n-1:-1:1
p(i,j)=aa1(j)*p(i,j+1)+bb1(j);
end
end %i
for j=1:n % Chegaraviy qiymatlarni hisoblash
p(n,j)=(4.0*p(n-1,j)-p(n-2,j))/3;
p(1,j)=(4.0*p(2,j)-p(3,j))/3;
end
for i=1:n % keyingi qatlam uchun boshlangich qiymatni hisoblash
for j=1:n
p0(i,j)=p(i,j);
end
end
end
function [p0,p] = fyx(n,tau,h,p0,x,y,t,jt)
% k+1 vaqt qatlamida hisoblash
for i=2:n-1 % Tenglamalar koeffitsientini hisoblash
for j=2:n-1
%chekli ayirma tenglama koeffitsentlarini hisoblash
a(j)=1;
c(j)=1;
b(j)=2+2*h*h/tau;
ff=h*h*(-2*(2*t(jt)*t(jt)*(x(i)*x(i)+y(j)*y(j))+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d(j)=2*h*h/tau*p0(i,j)+(p0(i-1,j)-2*p0(i,j)+p0(i+1,j))+ff;
end
%Pragonka koeffitsentlarini hisoblash
aa1(1)=0; bb1(1)=1;
for j=2:n-1
aa1(j)=c(j)/(b(j)-aa1(j-1)*a(j));
bb1(j)=(d(j)+bb1(j-1)*a(j))/(b(j)-aa1(j-1)*a(j));
end
% bosim funktsiyasini hisoblash
p(i,n)=exp(-2*x(i)*t(jt));
for j=n-1:-1:1
p(i,j)=aa1(j)*p(i,j+1)+bb1(j);
end
end %i
for j=1:n % Chegaraviy qiymatlarni hisoblash
p(n,j)=(4.0*p(n-1,j)-p(n-2,j))/3;
p(1,j)=(4.0*p(2,j)-p(3,j))/3;
end
for i=1:n % keyingi qatlam uchun boshlangich qiymatni hisoblash
for j=1:n
p0(i,j)=p(i,j);
end
end
% k+0.5 vaqt qatlamida hisoblash
for j=2:n-1 % Tenglamalar koeffitsientini hisoblash
for i=2:n-1
%chekli ayirma tenglama koeffitsentlarini hisoblash
a(i)=1;
c(i)=1;
b(i)=2+2*h*h/tau;
ff=h*h*(-2*(2*t(jt)*t(jt)*(x(i)*x(i)+y(j)*y(j))+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d(i)=2*h*h/tau*p0(i,j)+(p0(i,j-1)-2*p0(i,j)+p0(i,j+1))+ff;
end
%Pragonka koeffitsentlarini hisoblash
aa1(1)=0; bb1(1)=1;
for i=2:n-1
aa1(i)=c(i)/(b(i)-aa1(i-1)*a(i));
bb1(i)=(d(i)+bb1(i-1)*a(i))/(b(i)-aa1(i-1)*a(i));
end
% bosim funktsiyasini hisoblash
p(n,j)=exp(-2*y(j)*t(jt));
for i=n-1:-1:1
p(i,j)=aa1(i)*p(i+1,j)+bb1(i);
end
end %j
for i=1:n % Chegaraviy qiymatlarni hisoblash
p(i,n)=(4.0*p(i,n-1)-p(i,n-2))/3;
p(i,1)=(4.0*p(i,2)-p(i,3))/3;
end
for j=1:n % k+1 qatlam uchun boshlangich qiymatni hisoblash
for i=1:n
p0(i,j)=p(i,j);
end
end
end
The graph should look like this

採用された回答

Walter Roberson
Walter Roberson 2022 年 2 月 10 日
px(j)=p(21,j);
You do not define p anywhere in your script before that point.
You do define p strictly inside the functions fxy and fyx, and those functions offer to return p to the calling script or function. However, your calling script has
for jt=1:nt/2 % Vaqt bo`yicha tsikl}
fxy(n,tau,h,p0,x,y,t,jt);
fyx(n,tau,h,p0,x,y,t,jt);
end % jt
That code repeatedly calls the functions fxy and fxy and discards all of their outputs, not saving any of the calculated p0 or p values.
The p0 and p values that the functions offer to return are 2 dimensional arrays. You are calling the functions repeatedly, so you really should be saving the 2 dimensional arrays for each jt value -- so if you decide to store the returned p0 and p then you really should use a 3D array; it looks to me as if you should be using n x n x floor(nt/2) as the output size if you decide to save the returned values.
Then when you get to the loop below that,
for j=1:n
px(j)=p(21,j);
pxt(j)=pt(21,j);
end
if you have saved the return values from the functions, then is that p intended to refer to the return value from fxy? Is it intended to refer to the return value from fyx? Which particular value of jt is it intending to be using when you pull out the values in that for j loop ?
meshc(x,y,p) %Uch o'lchovli 3D grafika
if you have saved the return values from the functions, then is that p intended to refer to the return value from fxy? Is it intended to refer to the return value from fyx? Which particular value of jt is it intending to be using when you meshc() ? You cannot use them all because you (should have) saved a 3D array for p, one n x n array for each for jt value, and when you call meshc(), the third parameter needs to be a 2d array, not a 3d array.
contour(x,y,p,'ShowText','on','LineWidth',2); %kontur chizish
if you have saved the return values from the functions, then is that p intended to refer to the return value from fxy? Is it intended to refer to the return value from fyx? Which particular value of jt is it intending to be using when you contour() ? You cannot use them all because you (should have) saved a 3D array for p, one n x n array for each for jt value, and when you call contour(), the third parameter needs to be a 2d array, not a 3d array.
  6 件のコメント
Walter Roberson
Walter Roberson 2022 年 2 月 14 日
Generally speaking, code sometimes has this kind of structure
initial_value_of_variable = (some constant)
initial_value_of_something_else = (some other constant)
current_value_of_variable = initial_value_of_variable;
current_value_of_other_variable = initial_value_of_something_else
start a loop
some_variable = some_function(current_value_of_variable, current_value_of_other_variable);
some_other_variable = some_other_function(some_variable, current_value_of_other_variable);
current_value_of_variable = some_variable;
current_value_of_other_variable = some_other_variable;
end of loop
That is, you initialize some variables first, and then you refine the values inside the loop, and then set the refined value as being the "current" value that will become the input for the next cycle of the loop.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by