Bisection Method Piecewise function
조회 수: 3 (최근 30일)
이전 댓글 표시
So this is my code to try to bisect the piecewise function. But, when I run the code that was in the editor then I do
[r, iters] = bisectionF('f_feb15(x)', -2, .1)
matlab says
Undefined function or variable 'bisectionF'.
Sorry for such an elementary question. I am just beginning a scientific computing class and I am a math major so the only computer experience I have is LaTeX! Thank you so much for helping! I think it is something silly that I am missing. One of my friends said "I think it might have to do with your function file being saved with a different name then you actually called it." but I have no idea what this even means.
function[value]=f_feb15(x)
if x<=0
value=x^3+3*x+1
else if x>0
value=1+sin(x)
end %ends else if
end %ends else
clear all;
function[r, iters] = bisection(f, xL, xR)
for j=1:10
xM=(xL+xR)/2;
fM=feval(f, xM)
fL=feval(f, xL)
fR=feval(f, xR)
if fM*fL<0
xR=xM
else
xL=xM
end %end if
end %end for
if abs(fM)<10^(-5)
end %end if
end %function
댓글 수: 0
답변 (2개)
Walter Roberson
2017년 2월 17일
Your bisectionF should be in bisectionF.m
Your f_feb15 should be in f_feb15.m
You should delete the "clear all" and you should never use it inside a function again. If you use "clear all" inside a script, it should be the very last thing in the script, and it should be a script dedicated to trying to get your MATLAB session unstuck. "clear all" is the "blow EVERYTHING up, especially yourself" command.
Your call
[r, iters] = bisectionF('f_feb15(x)', -2, .1)
should be
[r, iters] = bisectionF(@f_feb15, -2, .1)
or
[r, iters] = bisectionF('f_feb15', -2, .1) %not really recommended
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!