Why doesn't his function return solution while using the switch command?

Viewed 38

I plan on using the switch command to use multiple chunks of code with differing ODE methods. Depending on what the user inputs as the 'diff' the code will run a specific chunk of code. Any ideas as to why this function isn't returning anything when I enter ODE(@(x,y)x^2,1,2,4,7,1) in to the command window? I've tried reassigning the output variables in the first line but to no avail.

function [x1,y1] = ODE(f,x0,y0,xn,h,diff)

%h = step size

switch diff
    case '1'
            h=(y0-x0)/xn; 
            y1=zeros(1,xn+1);
            x1=x0:h:y0;
            y1(1)=x0;
            for j=1:xn
                k1=feval(f,x1(j),y1(j));
                k2=feval(f,x1(j+1),y1(j)+h*k1);
                y1(j+1)=y1(j)+h*(h(1)+(h/2)*(k1+k2));
            end
            r=[x1',y1'];
            return        

end

1 Answers

Changing the line from case '1' to case 1 might do the trick. Alternatively you can call the function with a character input in the format of ODE(@(x,y)x^2,1,2,4,7,'1').

Switch Case for Characters:

diff = '1'; %Character input%

switch diff
    case 1     
        disp("diff is a number equal to 1");
    case '1'
        disp("diff is a character equal to '1'") 
end

Returns:

diff is a character equal to '1'


Switch Case for Numbers:

diff = 1; %Numerical input%

switch diff
    case 1     
        disp("diff is a number equal to 1");
    case '1'
        disp("diff is a character equal to '1'") 
end

Returns:

diff is a number equal to 1

Related