How to calcul equation using function Matlab
Show older comments
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 Comments
dpb
on 6 Dec 2020
There is no such array shaped other than as 2D rectangular array so you have no way to store your z variable as a single array in such a form. You would have to pick a storage order and index in some fashion or fill in the missing corner values with NaN or somesuch.
The simplest way to do what you're asking would be to use meshgrid and evaluate the function over the entire outer boundaries first, then reset the upper quadrant to NaN to indicate the missing locations.
If you don't want to return those in that fashion, then at that point you could make the storage transformation to whatever holding arrangement you did choose.
What is the intended use of the result? That could help in suggesting storage alternatives.
Yamina chbak
on 6 Dec 2020
Edited: Yamina chbak
on 6 Dec 2020
Yamina chbak
on 6 Dec 2020
John D'Errico
on 6 Dec 2020
Edited: John D'Errico
on 6 Dec 2020
There should be NO problem. Just compute it on the entire domain, and then set the elements outside the L-shaped boundary to NaN. So where is the disconnect?
Can you compute the result on the entire domain? A simple vectorized computation would do that, in as little as one line of code. Then, can you set the elements to NaN in a corner?
Yamina chbak
on 6 Dec 2020
Edited: Yamina chbak
on 6 Dec 2020
dpb
on 6 Dec 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
on 6 Dec 2020
Accepted Answer
More Answers (0)
Categories
Find more on Gamma Functions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!