Hello there, I have a function f(x,t) defined on (0,1). The function is however discontinuous at x=1/2 such that we have two separate values f1(x,t) for x in (0,1/2) and f2(x,t) for x in (1/2, 1). How do I program this function with M function file that takes input x and t?. Arbitrary function can be used to explain. Thanks a lot

댓글 수: 4

Ced
Ced 2016년 3월 25일
Just use an if statement checking in what domain your input is, and evaluate accordingly.
Oluwaseun Lijoka
Oluwaseun Lijoka 2016년 3월 25일
편집: Walter Roberson 2016년 3월 25일
Hello Ced, here is what I did. But I was getting error "Output argument "f" (and maybe others) not assigned during call to "exact_soln". Any idea as to why?. Thanks a lot.
function f = exact_soln(x,t,c_1,c_2)
% c_1 and c_2 are velocities on separate domain (0,1/2) and (1/2,1)
fn=@(x)sin(2*pi*x);
if x(x<=1/2)%
f= 1/2*(fn(x+c_1*t) + fn(x-c_1*t));
end
if x(1/2<=x)
f= 1/2*(fn(x+c_2*t) + fn(x-c_2*t));
end
Walter Roberson
Walter Roberson 2016년 3월 25일
Is x a scalar or a vector?
Oluwaseun Lijoka
Oluwaseun Lijoka 2016년 3월 26일
x is allowed to be a vector. Mostly computed via quadrature.@Robertson

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

 채택된 답변

Ced
Ced 2016년 3월 25일
편집: Ced 2016년 3월 25일

0 개 추천

This means that you have defined a function with an output (in your case the variable f), but have not assigned it. This would happen if x does not fall into any of your cases.
Your if conditions are a bit strange.
(x<=1/2) will return 0 if false, 1 if true. So, if e.g. x = 0, then x(x<=1/2) = x(1) = 0, which is considered false instead of true.
This should work better:
function f = exact_soln(x,t,c_1,c_2)
fn=@(x)sin(2*pi*x);
if x <= 0.5 % domain section 1
f= 0.5*(fn(x+c_1*t) + fn(x-c_1*t));
else % domain section 2, 0.5 < x
f= 0.5*(fn(x+c_2*t) + fn(x-c_2*t));
end
end
Having an else statement ensures that no matter x, f will always get a value.
Note that the boundaries x == 0 and x == 1 are actually not checked! If you need your wave to stop there, you could either add more if/else statements, or simply bound x before hand, i.e.
x = max(min(x,1),0);

댓글 수: 1

Oluwaseun Lijoka
Oluwaseun Lijoka 2016년 3월 25일
Thanks Ced, I actually extended the solution by f=0 elsewhere before but your idea is simple and better. I also like the idea of bounding x as well. Thanks a lot.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2016년 3월 25일

댓글:

2016년 3월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by