Using nested if statements in matlab

124 Views Asked by At
function methods = prj2()
a = input('enter the function', 's');
f = inline(a);
itr = 0;
lim = 0.00001;
c = input('enter the method number, 1 for secant, 2 for newton, 3 for QII');

    if (c == 1)

        x(1) = input('enter the first guess');
        x(2) = input('enter the second guess');
        error = x(2)+lim;
            while (error>lim)
                x(i+1)=x(i)-f(x(i))*(x(i)-x(i-1))/(f(x(i))-f(x(i-1)));
                i = 2;
                i = i + 1;
                itr = itr + 1;
                root = x(i)
            end
        disp(sprintf('Answer= %0.8g', root))
    else if (c == 2)
        syms x(1) = input('enter the initial x');
        error = abs(x(i+1)-x(i));
        i = 1;
            while (error > lim)
                x(i+1)=x(i)-((f(x(i)))/(diff(f(x(i)))));
                itr = itr + 1;
                i = i + 1;
                root = x(i);
            end
    else if (c==3)
        error = abs(x(i+1)-x(i));
        i = 1;
        x(1) = input('enter the first x');
        x(2) = input('enter the second x');
        x(3) = input('enter the third x');
        while (error > lim)
            a = ((f(x(i+1))*f(x(i)))/((f(x(i+2))-(f(x(i+1))))*(f(x(i+2))-f(x(i)))))*(x(i+2));
            b = ((f(x(i+2))*f(x(i)))/((f(x(i+1))-(f(x(i+2))))*(f(x(i+1))-f(x(i)))))*(x(i+1));
            c = ((f(x(i+1))*f(x(i+2)))/((f(x(i))-(f(x(i+1))))*(f(x(i))-f(x(i+2)))))*(x(i));
            f(x(i+3)) = a + b + c;
            i = i + 1;
            itr = itr + 1;
            root = x(i);
            break
        end
    end
end

Hello. The above is my matlab code for finding root. User inputs the value of c, (from 1 to 3), which determines the method to use to find the root (Newton's method, secant method, and Quadratic Inverse Interpolation).

I wanted to keep everything under one file, and I am new to MATLAB, and haven't started reading its manual, but I am not so sure why the code whenever I run gets error message that variable root is not defined.

2

There are 2 best solutions below

2
On

For example, if $c = 1$ and the initial $error \le lim$, $root$ is never assigned a value before you try to display it.

That's not the only problem with your code. For example, $error$ and $lim$ are not assigned values in the loop, so if $error > lim$ the loop will never end.

0
On
function methods = prj3()
a = input('enter the function', 's');
f = inline(a);
itr = 0;
lim = 0.00001;
c = input('enter the method number, 1 for secant, 2 for newton, 3 for QII');
error = 1;
while (error>lim)
    i = 1;
    if (c == 1)
        x(1) = input('enter the first guess');
        x(2) = input('enter the second guess');
        while (error>lim)
            x(i+2)=x(i+1)-(f(x(i+1))*(x(i+1)-x(i))/(f(x(i+1))-f(x(i))));
            i = i + 1;
            error = x(i+1)-x(i);
            itr = itr + 1
            root = x(i+1)
        end
    elseif (c == 2)
        x(1) = input('enter the initial x');

        while (error > lim)
            x(i+1)=x(i)-((f(x(i)))/diff(f(x(i))));
            error = abs(x(i+1)-x(i));
            itr = itr + 1;
            i = i + 1;
            root = x(i);
        end
    elseif (c == 3)
        x(1) = input('enter the first x');
        x(2) = input('enter the second x');
        x(3) = input('enter the third x');
        while (error > lim)
            a = ((f(x(i+1))*f(x(i)))/((f(x(i+2))-(f(x(i+1))))*(f(x(i+2))-f(x(i)))))*(x(i+2));
            b = ((f(x(i+2))*f(x(i)))/((f(x(i+1))-(f(x(i+2))))*(f(x(i+1))-f(x(i)))))*(x(i+1));
            c = ((f(x(i+1))*f(x(i+2)))/((f(x(i))-(f(x(i+1))))*(f(x(i))-f(x(i+2)))))*(x(i));
            x(i+3) = a + b + c;
            i = i + 1;
            error = abs(x(i+1)-x(i));
            itr = itr + 1
            root = x(i)
        end
    else
        disp(sprintf('no'));
    end    
end