the code wont display an answers calculated

2 views (last 30 days)
so i have a code that has multiple nested if statements. before calculations are run the user is asked for a method f,b or c and if they choose f and b it works when c is chosen nothing is displayed. but the code seems identical. can anyone help?
function deraproxx %This program will find an approximate solution to the nth order %derrivative of an nth order polynomial where n is 1-4
disp ('Please input a polynomial function') disp ('please use the following format') disp('a*x.^4+b*x.^3+c*x.^2+d*x+e') disp('enter 0 if there is no coefficient to one of the variables') f=input('type now ','s');
disp ('what is the order of derrivation you desire?') n=input('type now ');
disp ('what is the number of terms in taylor series? (1 or 2)') tn=input('type now ');
disp ('what method would you like to use? f, b, or c') method=input('type now ','s');
disp(' what is the step size?') h=input('type now ');
disp('what point would you like to evaluate?') xi=input ('type now ');
x=((xi-5*h):h:(xi+5*h)); y=eval(f);
fxim5=y(1,1); fxim4=y(1,2); fxim3=y(1,3); fxim2=y(1,4); fxim1=y(1,5); fxi=y(1,6); fxip1=y(1,7); fxip2=y(1,8); fxip3=y(1,9); fxip4=y(1,10); fxip5=y(1,11);
if method=='f' if n==1 if tn==1 sna=(fxip1-fxi)/h disp(sna) else sna=(-fxip2+4*fxi+1-3*fxi)/2*h disp(sna) end elseif n==2 if tn==1 sna=(fxip2-2*fxip1+fi)/(h*h) disp(sna) else sna=(-fxip3+4*fxip2-5*fxip1+2*fxi)/(h*h) disp(sna) end elseif n==3 if tn==1 sna=(fxip3-3*fxip2+3*fxip1-fxi)/(h*h*h) disp(sna) else sna=(-3*fxip4+14*fxp3-24*fxip2+18*fxip1-5*fxi)/(2*h*h*h) disp(sna) end else if tn==1 sna=(fxip4-4*fxip3+6*fxp2-4*fxip1-fxi)/(h*h*h*h) disp(sna) else sna=(-2*fxip5+11*fxip4-24*fxip3+26*fxip2-14*fxip1+3*fxi)/(h*h*h*h) disp(sna) end end elseif method=='b' if n==1 if tn==1 sna=(fxi-fxim1)/h disp(sna) else sna=(3*fxi-4*fxim1+fxim2)/2*h disp(sna) end elseif n==2 if tn==1 sna=(fxi-2*fxim1+fxim2)/(h^2) disp(sna) else sna=(2*fxi-5*fxim1+4*fxim2-fxim3)/(h^2) disp(sna) end elseif n==3 if tn==1 sna=(fxi-3*fxim1+3*fxim2-fxim3)/(h^3) disp(sna) else sna=(5*fxi-18*fxim1+24*fxim2-14*fxim3+3*fxim4)/(2*(h^3)) disp(sna) end elseif n==4 if tn==1 sna=(fxi-4*fxim1+6*fxim2-4*fxim3+fxim4)/(h^4) disp(sna) else sna=(3*fxi-14*fxim1+26*fxim2-24*fxim3+11*fxim4-2*fxim5)/(h^4) disp(sna) end else if n==1 if tn==1 sna=(fxip1-fxim1)/(h*2) disp(sna) else sna=(-fxip2+8*fxip1-8*fxim1+fxim2)/(12*h) disp(sna) end elseif n==2 if tn==1 sna=(fxip1-2*fxi_fxim1)/(h*h) disp(sna) else sna=(-fxip2+16*fxip1-30*fxi+16*fxim1-fxim2)/(12*h*h) disp(sna) end elseif n==3 if tn==1 sna=(fxip2-2*fxip1+2*fxim1-fxim2)/(2*h*h*h) disp(sna) else sna=(-fxip3+8*fxip2-13*fxip1+133*fxim1-8*fxim2+fxim3)/(8*h*h*h) disp(sna) end else if tn==1 sna=(fxip2-4*fxip1+6*fxi-4*fxim1+fxim2)/(h*h*h*h) disp(sna) else sna=(-fxip3+12*fxip2-39*fxip1+56*fxi-39*fxim1+12*fxim2-fxim3)/(64*h*h*h*h) disp(sna) end end end end

Answers (1)

David Sanchez
David Sanchez on 3 Aug 2015
Hi there, after reordering your code, I saw it follows this structure:
method=='f'
--> n= 1,2,3,other_value
method=='b'
--> n=1,2,3,4,other_value -> again an if-statement for n=1,2,3,other_value
you do not have the case for
method=='c'
  3 Comments
David Sanchez
David Sanchez on 3 Aug 2015
it doesn't work the way you did it. Go through your code carefully and you'll find out that you do not have an option when method=='c'

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!