in MATLAB turn this ifelse statement into switch case statem

in MATLAB turn this if-else statement into switch case statement function[root1,root2]=HW3P3_1(a,b,c) y=b^2-4*a*c; if y<0 disp(\'Error! Negative root!\') else root1=(-b+sqrt(y))/(2*a); root2=(-b-sqrt(y))/(2*a); end fprintf(\'Root 1 = %10.5f \ \',root1) fprintf(\'Root 2 = %10.5f \ \',root2)

Solution

function[root1,root2]=HW3P3_1(a,b,c)

switch(b^2<4*a*c) %% y<0 implies b^2<4*a*c. when this logical expression is true, argument inside switch takes value 1, otherwise 0

case 1

  disp(\'Error! Negative root!\')

otherwise

  root1=(-b+sqrt(y))/(2*a);

  root2=(-b-sqrt(y))/(2*a);

end

fprintf(\'Root 1 = %10.5f \ \',root1)

fprintf(\'Root 2 = %10.5f \ \',root2)

in MATLAB turn this if-else statement into switch case statement function[root1,root2]=HW3P3_1(a,b,c) y=b^2-4*a*c; if y<0 disp(\'Error! Negative root!\') els

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site