How to calcul equation using function Matlab

조회 수: 1 (최근 30일)
Yamina chbak
Yamina chbak 2020년 12월 6일
편집: Yamina chbak 2020년 12월 7일
Hi,
I try to write a function for calcul eqution u(x,y t)=exp(-t)*sin(pi*x/L)*sin(pi*y/L) on domaine L-shaped
function z=U(x,y,time,L)
x=[0;0;0;0;0;0;0;0;0;0;0;1;2;3;4;5;6;7;8;9;10;10;10;10;10;10;9;8;7;6;5;5;5;5;5;5;4;3;2;1;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;1;2;3;4;1;2;3;4;1;2;3;4;1;2;3;4;1;2;3;4];
y=[0;1;2;3;4;5;6;7;8;9;10;0;0;0;0;0;0;0;0;0;0;1;2;3;4;5;5;5;5;5;5;6;7;8;9;10;10;10;10;10;1;1;1;1;1;1;1;1;1;2;2;2;2;2;2;2;2;2;3;3;3;3;3;3;3;3;3;4;4;4;4;4;4;4;4;4;5;5;5;5;6;6;6;6;7;7;7;7;8;8;8;8;9;9;9;9];
z=zeros(size(x));
time=0.15; L=10; E=exp(-time)
%gamma1
if(x==0 & y>=0&y<=L)
z=E.*sin(pi*y(x==0&y>=0&y<=L)/L).*sin(pi*0/L);
%gamma2
elseif( y==L & x>0&x<=L/2)
z=E.*sin(pi*x(y==L&x>0&x<=L/2)/L).*sin(pi*L/L);
%gamma3
elseif( x==L/2 & y>=L/2&y<L)
z=E.*sin(pi*y(x==L/2&y>=L/2&y<L)/L).*sin(pi*5/L);
%gamma4
elseif( y==L/2 & x>L/2&x<=L)
z=E.*sin(pi*x(y==L/2&x>L/2&x<=L)/L).*sin(pi*5/L)
%gamma5
elseif (x==L & y>=0&y<L/2)
z=E.*sin(pi*y(x==L&y>=0&y<L/2)/L).*sin(pi*10/L);
%gamma6
elseif( y==0 & x>0&x<L)
z=E.*sin(pi*x(y==0&x>0&x<L)).*sin(pi*0/L);
%gamma7
% elseif ??? I don't how to write in this case !
z=E.*sin(pi*x/L).*sin(pi*y/L);
end
end
Please Help me.
Thanks
  댓글 수: 7
dpb
dpb 2020년 12월 6일
"... you mean compute it on, for example, square domain ? And set the elements outside the L-shaped to NaN?"
That's precisely what we meant, yes. There's certainly no easier way to compute the complete domain and you can extract the boundary segment values from it if returning those is another desire.
Those alone could be computed far more efficiently by just computing along the specific lines without all the logical addressing for whatever spacing along the x,y axes is desired.
Your function as written doesn't have any way to return but the last z vector because each clause in the if...elseif...end block writes to the same variable.
The if block isn't properly constructed; in MATLAB an IF is satisfied IFF all elements of the expression are true; your x and y being arrays will return an array of logicals only a few of which will be T. Hence, none of the expressions will be satisfied.
Lastly, there's no point in having x,y,time,L as arguments in the function since you immediately redefine them inside the function.
Yamina chbak
Yamina chbak 2020년 12월 6일
I get your point, I will try it if i have no choice, I can compute the resultat on the square domain but i don't know how to set the elements outside the L-shaped to NaN in a corner!. I am a beginner at Matlab.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 6일
function z=U(x,y,time,L)
x=[0;0;0;0;0;0;0;0;0;0;0;1;2;3;4;5;6;7;8;9;10;10;10;10;10;10;9;8;7;6;5;5;5;5;5;5;4;3;2;1;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;1;2;3;4;1;2;3;4;1;2;3;4;1;2;3;4;1;2;3;4];
y=[0;1;2;3;4;5;6;7;8;9;10;0;0;0;0;0;0;0;0;0;0;1;2;3;4;5;5;5;5;5;5;6;7;8;9;10;10;10;10;10;1;1;1;1;1;1;1;1;1;2;2;2;2;2;2;2;2;2;3;3;3;3;3;3;3;3;3;4;4;4;4;4;4;4;4;4;5;5;5;5;6;6;6;6;7;7;7;7;8;8;8;8;9;9;9;9];
z=zeros(size(x));
time=0.15; L=10; E=exp(-time)
%gamma1
mask = (x==0 & y>=0&y<=L)
z(mask) = E.*sin(pi*y(mask)/L).*sin(pi*0/L);
%gamma2
mask = ( y==L & x>0&x<=L/2)
z(mask) = E.*sin(pi*x(mask)/L).*sin(pi*L/L);
%gamma3
mask = ( x==L/2 & y>=L/2&y<L)
z = E.*sin(pi*y(mask)/L).*sin(pi*5/L);
and so on.
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 12월 7일
function z=U(x,y,time,L)
x=[0;0;0;0;0;0;0;0;0;0;0;1;2;3;4;5;6;7;8;9;10;10;10;10;10;10;9;8;7;6;5;5;5;5;5;5;4;3;2;1;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;1;2;3;4;1;2;3;4;1;2;3;4;1;2;3;4;1;2;3;4];
y=[0;1;2;3;4;5;6;7;8;9;10;0;0;0;0;0;0;0;0;0;0;1;2;3;4;5;5;5;5;5;5;6;7;8;9;10;10;10;10;10;1;1;1;1;1;1;1;1;1;2;2;2;2;2;2;2;2;2;3;3;3;3;3;3;3;3;3;4;4;4;4;4;4;4;4;4;5;5;5;5;6;6;6;6;7;7;7;7;8;8;8;8;9;9;9;9];
z=zeros(size(x));
time=0.15; L=10; E=exp(-time)
%gamma1
mask1 = (x==0 & y>=0&y<=L)
z(mask1) = E.*sin(pi*y(mask1)/L).*sin(pi*0/L);
%gamma2
mask2 = ( y==L & x>0&x<=L/2)
z(mask2) = E.*sin(pi*x(mask2)/L).*sin(pi*L/L);
%gamma3
mask3 = ( x==L/2 & y>=L/2&y<L)
z = E.*sin(pi*y(mask3)/L).*sin(pi*5/L);
etc
mask7 = ~(mask1 | mask2 | mask3 | mask4 | mask5 | mask6);
z(mask7) = E.*sin(pi*x(mask7)/L).*sin(pi*y(mask7)/L);
Yamina chbak
Yamina chbak 2020년 12월 7일
편집: Yamina chbak 2020년 12월 7일
Thanks you @Walter Roberson. It's working.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by