How to calcul equation using function Matlab

2 vues (au cours des 30 derniers jours)
Yamina chbak
Yamina chbak le 6 Déc 2020
Modifié(e) : Yamina chbak le 7 Déc 2020
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 commentaires
dpb
dpb le 6 Déc 2020
"... 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 le 6 Déc 2020
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.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 6 Déc 2020
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 commentaires
Walter Roberson
Walter Roberson le 7 Déc 2020
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 le 7 Déc 2020
Modifié(e) : Yamina chbak le 7 Déc 2020
Thanks you @Walter Roberson. It's working.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by