Having issue displaying the result of a set of if statements

1 次查看(过去 30 天)
Hello i have an issue i want to display the variable "ans" im using disp(ans) and ive placed it at every point possible it doesnt seem to work. the code is as follows
function deraprox
%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
ans=(fxip1-fxi)/h
else
ans=(-fxip2+4*fxi+1-3*fxi)/2*h
end
elseif n==2
if tn==1
ans=(fxip2-2*fxip1+fi)/(h*h)
else
ans=(-fxip3+4*fxip2-5*fxip1+2*fxi)/(h*h)
end
elseif n==3
if tn==1
ans=(fxip3-3*fxip2+3*fxip1-fxi)/(h*h*h)
else
ans=(-3*fxip4+14*fxp3-24*fxip2+18*fxip1-5*fxi)/(2*h*h*h)
end
else
if tn==1
ans=(fxip4-4*fxip3+6*fxp2-4*fxip1-fxi)/(h*h*h*h)
else
ans=(-2*fxip5+11*fxip4-24*fxip3+26*fxip2-14*fxip1+3*fxi)/(h*h*h*h)
end
end
elseif method=='b'
if n==1
if tn==1
ans=(fxi-fxim1)/h
else
ans=(3*fxi-4*fxim1+fxim2)/2*h
end
elseif n==2
if tn==1
ans=(fxi-2*fxim1+fxim2)/(h^2)
else
ans=(2*fxi-5*fxim1+4*fxim2-fxim3)/(h^2)
end
elseif n==3
if tn==1
ans=(fxi-3*fxim1+3*fxim2-fxim3)/(h^3)
else
ans=(5*fxi-18*fxim1+24*fxim2-14*fxim3+3*fxim4)/(2*(h^3))
end
elseif n==4
if tn==1
ans=(fxi-4*fxim1+6*fxim2-4*fxim3+fxim4)/(h^4)
else
ans=(3*fxi-14*fxim1+26*fxim2-24*fxim3+11*fxim4-2*fxim5)/(h^4)
end
else
if n==1
if tn==1
ans=(fxip1-fxim1)/(h*2)
else
ans=(-fxip2+8*fxip1-8*fxim1+fxim2)/(12*h)
end
elseif n==2
if tn==1
ans=(fxip1-2*fxi_fxim1)/(h*h)
else
ans=(-fxip2+16*fxip1-30*fxi+16*fxim1-fxim2)/(12*h*h)
end
elseif n==3
if tn==1
ans=(fxip2-2*fxip1+2*fxim1-fxim2)/(2*h*h*h)
else
ans=(-fxip3+8*fxip2-13*fxip1+133*fxim1-8*fxim2+fxim3)/(8*h*h*h)
end
elseif n==4
if tn==1
ans=(fxip2-4*fxip1+6*fxi-4*fxim1+fxim2)/(h*h*h*h)
else
ans=(-fxip3+12*fxip2-39*fxip1+56*fxi-39*fxim1+12*fxim2-fxim3)/(64*h*h*h*h)
end
end
end
end

回答(1 个)

Walter Roberson
Walter Roberson 2015-8-3
You should name your variable something other than "ans" as MATLAB uses ans for its own purposes.
You should look at how to debug

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by