What is missing in this code to solve the problem?

&nbsp
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 Comment

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.

Sign in to comment.

Answers (1)

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 Comment

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.

Sign in to comment.

Categories

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

Asked:

on 27 Nov 2014

Commented:

on 28 Nov 2014

Community Treasure Hunt

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

Start Hunting!