how to write a function with loop for and if-statement ?

Hi, I need to write a function U like this :
if x=0 then U(0,y)=0
if y=0 then U(x,0)=0
if y=10 then U(x,10)=sin(pi*x/L).*sin(pi*10/L) for x=[0,L/2]
if x=10 then U(10,y)=sin(pi*y/L).*sin(pi*10/L) for y=[0,L/2]
if y=5 then U(x,5)=sin(pi*x/L).*sin(pi*5/L) for x=[L/2,L]
if x=5 then U(5,y)=sin(pi*y/L).*sin(pi*5/L) for y=[L/2,L]
So, i have a vector of x and y, for example x=[0 0 0 0 1 ....5....10..0..1..5....0] and y=[0 0 0 0 1 ....5....10..0..1..5....0], and npt=size(x)=size(y). I did code about it but it not work
function z= U(npt,x,y,L)
for m=1:npt
if( x(m)==0)
z=0;
end
end
for m=1:npt
if x(m)==L
z= sin(pi*y/L).*sin(pi*10/L);
end
end
for m=1:npt
if y(m)==0
z=0;
end
end
for m=1:npt
if y(m)==L
z = sin(pi*x/L).*sin(pi*10/L);
end
end
for m=1:npt
if x(m)==L/2
for y(m)=L/2:L
z= sin(pi*5/L).*sin(pi*y/L);
end
end
end
for m=1:npt
if y(m)==L/2
for x(m)=L/2:L
z = sin(pi*5/L).*sin(pi*x/L);
end
end
end
end

 Accepted Answer

function z= U(x,y,L)
U=zeros(size(x));
U(x==10&y>=0&y<=L/2)=sin(pi*y(x==10&y>=0&y<=L/2)/L)*sin(pi*10/L);
U(y==10&x>=0&x<=L/2)=sin(pi*x(y==10&x>=0&x<=L/2)/L)*sin(pi*10/L);
U(x==5&y>=L/2&y<=L)=sin(pi*y(x==5&y>=L/2&y<=L)/L)*sin(pi*5/L);
U(y==5&x>=L/2&x<=L)=sin(pi*x(y==5&x>=L/2&x<=L)/L)*sin(pi*5/L);
end

5 Comments

Thanks you David Hill for answer me. It's good, but it's not working for my other code, I just edit this code, but its give me results equal 0
function z= U(x,y)
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];
L=10;
% z=zeros(size(x));
if x==L & y>=0 & y<=L/2
z=sin(pi*y(y>=0&y<=L/2)/L).*sin(pi*x/L);
end
if y==L & x>=0 & x<=L/2
z=sin(pi*x(x>=0&x<=L/2)/L).*sin(pi*y/L);
end
if x==L/2 & y>=L/2 & y<=L
z=sin(pi*y(y>=L/2&y<=L)/L).*sin(pi*x/L);
end
if y==L/2 & x>=L/2 & x<=L
z=sin(pi*x(x>=L/2&x<=L)/L).*sin(pi*y/L);
end
if x==0 & x>=0 & x<=L
z=0;
end
if y==0 & y>=0 & y<=L
z=0;
end
end
@Ahdia Achabbak: don't use if, use logical indexing as David Hill showed.
@David Hill , Thanks you David, It's working for me and I understand well what you use logical indexing. And also thanks you @Stephen Cobeldick.
I would be grateful if you could help me with the same question, what if i write a function U(x,y,t)
function z= U(x,y,time,L)
z=zeros(size(x));
z(x==10&y>=0&y<=L/2)=exp(-time).*sin(pi*y(x==10&y>=0&y<=L/2)/L)*sin(pi*10/L);
z(y==10&x>=0&x<=L/2)=exp(-time).*sin(pi*x(y==10&x>=0&x<=L/2)/L)*sin(pi*10/L);
z(x==5&y>=L/2&y<=L)=exp(-time).*sin(pi*y(x==5&y>=L/2&y<=L)/L)*sin(pi*5/L);
z(y==5&x>=L/2&x<=L)=exp(-time).*sin(pi*x(y==5&x>=L/2&x<=L)/L)*sin(pi*5/L);
end
When I write
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];
time=0.15;
L=10;
g(:,time)=feval(@U,x,y,time,L);
I get "Subscripted assignment dimension mismatch" error
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];
time=0.15;
L=10;
g=U(x,y,time,L);
Thanks you David for help me ! It works.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2016b

Community Treasure Hunt

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

Start Hunting!