What is missing in this code to solve the problem?
이전 댓글 표시

 
A periodic function with period 3 is defined on interval 0=<x=<3 f(x)= 2x^0.5 for 0=<x=<1 and 3-x for 1=<x=<3. Write a function which given any real x returns f(x). Write a short routine which plots the graph of f in the range -6=<x=<6
댓글 수: 1
Geoff Hayes
2014년 11월 27일
편집: Geoff Hayes
2014년 11월 27일
Martin - this seems like a homework question, so what have you tried so far? Please post the code that you have written in an attempt to solve this problem, and include any errors/problems that you are experiencing with it.
답변 (1개)
Roger Stafford
2014년 11월 28일
If your image shows the code you tried, then here are some objections I have:
1) The defined function 'fal' needs to be terminated by 'end' or 'return' so that it is separate from the code that follows. Otherwise Matlab won't know the function part has ended.
2) You are calling your function with a vector, x, and therefore you cannot use a simple if-elseif-end construct to handle the elements of this vector. The line
x = x - floor(x/3);
is all right as a vector, but you will have to apply your 'if' conditions either using a for-loop to handle each element of x one-at-a-time, or use a logical vector method, such as
x = (x<=1).*(2*sqrt(x))+(x>1).*(3-x);
3) In the line "if (x>=0) & (x<=1" you don't need the "x>=0" since negative values of x have been eliminated by the line "x=x-3*floor(x/3)". Similarly, you don't need an "elseif" with conditions but can use "else", since the only remaining x values must necessarily satisfy those conditions.
4) In 3) above, if, after all, it had been necessary to use the 'and' operation and if you use the vectorized method in 2), then you could not use '&&', but must use '&', because '&&' cannot be used with vectors.
5) In calculating 'y' you used uppercase 'Y' which designates a different variable. Matlab is sensitive to case.
6) In calling your function you wrote "Y=f" but it should have been
y = fal(x);
in which your function is specifically named. Otherwise Matlab doesn't know what function you are referring to.
7) The "y = fal(x)" line needs to be placed AFTER x has been defined, not before.
댓글 수: 1
Image Analyst
2014년 11월 28일
A clarification on (1). Functions do not need an "end" statement. However if you have multiple functions defined in one m-file then they either should all NOT have an end, or all functions should have an end. You can't have some with end and others without end. If it's just one function in the m-file, then it does not need an end. I usually do not use end statements to terminate functions.
For (6), I think Martin is creating a new variable called y for plotting terminology purposes, not trying to call fal(). He wants it to be called y instead of f because he just likes the x,y terminology better I guess.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!