F. Marshall
F. Marshall

Reputation: 13

Piecewise functions in MATLAB

I have only recently began working with MATLAB for Uni this year, and as a homework quiz question I was asked to create a piecewise function (that I have called "piecewise_method") that is able to enact a different equation on "x" depending on if "x" is below 0, between 0 and 8, or above 8. Here is the code I have written so far.

function solution = piecewise_method(x)

% given the value of the input x, the function
% piecewise_method will choose from one of the three specified
% equations to enact upon the given value of x
% and give the user a solution

solution = zeros(size(x));
e = exp(1);

for j = 1:size(x)
    a = x(j);

    if a < 0
        solution(j) = -a.^3 - 2*a.^2 + 3*a;
    elseif (a >= 0) && (a <= 8)
        solution(j) = (12/pi)*sin(pi*a./4);
    else
        solution(j) = ((600.*e.^(a-8))./(7*(14+6.*e.^(a-8))) - 30/7);
    end
end

When run with the input...

x = -3:12

It produces this result for the variable solution...

solution =

 0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

Now this suggests to me that the array is being properly created, but for one reason or another the for loop isn't running properly, or as expected. I tried reconstructing the for loop a number of times from the basic level up, but when I started putting in the equations it began to fall apart again, so I believe something could be wrong with my equations (which is why I put brackets everywhere, just in case).

The question also asks me to use if statements, so I can't try using other methods to produce the piecewise method function, and from my searching around, there didn't seem to be many examples of if statements in piecewise functions.

If you could provide any suggestions that could help me with this function that would be greatly appreciated, thank you!

P.S. If there are any suggestions you have for improving my questions in the future that would be good too!

Upvotes: 1

Views: 896

Answers (2)

Wolfie
Wolfie

Reputation: 30046

As stated in comments and RadioJava's answer, you need to check your loop. I would use size(x,2) (number of columns in x) or numel(x) (number of elements in x).

for j = 1:numel(x)
    % ...

Others recommend length(x) which is max(size(x)). I generally try to avoid this because it's not explicit which dimension you want to look along for matrices.

Aside from just repeating info though, I want to show you could do this much more efficiently with logical indexing, and remove the problematic loop entirely...

function solution = piecewise_method(x)

% given the value of the input x, the function
% piecewise_method will choose from one of the three specified
% equations to enact upon the given value of x
% and give the user a solution

solution = zeros(size(x));
e = exp(1);

idx = (x<0);
solution(idx) = -x(idx).^3 - 2*x(idx).^2 + 3*x(idx);
idx = (x>=0 & x<=8);
solution(idx) = (12/pi)*sin(pi*x(idx)/4);
idx = (x>8);
solution(idx) = (600*e.^(x(idx)-8))./(7*(14+6*e.^(x(idx)-8))) - 30/7;

Note you could easily have conditions and (anonymous) functions in arrays and loop over them to make this much more flexible.

Upvotes: 0

Prostagma
Prostagma

Reputation: 1851

You should be using length instead of size at the for loop.

The output of size function is the dimensions of x, which for your example of x=-3:12 returns size(x)=[1 16]. Then your for loop will run for j=1:size(x), i.e j=1:1, i.e j=1.

The output of length is the length of the largest array dimension of x, as listed here. In your example: length(x) = 16, then j=1:length(x)=1:16.

Or, you could use size(x,2), which will return the size of the 2nd dimension of x, same as length in this case.

Upvotes: 2

Related Questions