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

3 visualizaciones (últimos 30 días)
Yamina chbak
Yamina chbak el 7 de Nov. de 2020
Comentada: Yamina chbak el 19 de Nov. de 2020
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

Respuesta aceptada

David Hill
David Hill el 7 de Nov. de 2020
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 comentarios
David Hill
David Hill el 9 de Nov. de 2020
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 el 19 de Nov. de 2020
Thanks you David for help me ! It works.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by