필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Yamina chbak
Yamina chbak 2020년 11월 7일
댓글: Yamina chbak 2020년 11월 19일
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

채택된 답변

David Hill
David Hill 2020년 11월 7일
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
David Hill
David Hill 2020년 11월 9일
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);
Yamina chbak
Yamina chbak 2020년 11월 19일
Thanks you David for help me ! It works.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by