Prev: memory restriction?
Next: Schwarz Christoffel Help
From: Dylan Dustin on 29 Apr 2010 22:37 I am required to write a function to find the zeros of a function, fx=x^3-5x^2+3x+2, by making a simple search algorithm that goes back and forth decreasing its step size to find the zeros. Does anyone have any advice on how to do this? Here is my code so far, just ignore the if statements I will explain them after. function [y]=finalproject1(x) y=1; x2=x-1; dx=7/100; for z=1:1:100 while (abs(y)>1e-10) y=feval('fun',x); if x&&x2>0||x&&x2<0 if x2/x>1 x2=x+dx; x=x2; else x=x-(x/2); end end end break end fun is a function containing my function. The if statements decide whether or not to switch directions and step size based on the ratio between two consecutive functions as follows: If ratio of consecutive values (and both have same sign), f (xk )/f (xk −1 ), is greater then 1 then switch direction of search 􏰀 If ratio of consecutive values (and both have same sign) is less that 1, then go the same direction with same step size 􏰀 If consecutive values are of opposite sign then switch directions and reduce the step size Thanks
From: Steven Lord on 30 Apr 2010 10:32 "Dylan Dustin" <dcb992005(a)gmail.com> wrote in message news:hrdfoh$npm$1(a)fred.mathworks.com... >I am required to write a function to find the zeros of a function, >fx=x^3-5x^2+3x+2, by making a simple search algorithm that goes back and >forth decreasing its step size to find the zeros. Does anyone have any >advice on how to do this? Here is my code so far, just ignore the if >statements I will explain them after. > > function [y]=finalproject1(x) > y=1; > x2=x-1; > dx=7/100; > for z=1:1:100 > while (abs(y)>1e-10) > y=feval('fun',x); Why use FEVAL? Why not just call fun directly? y = fun(x); > if x&&x2>0||x&&x2<0 I don't think this does what you think it does. This IF condition will be true if both x and x2 are nonzero, even if they have different signs. I believe you were expecting it to be true if x and x2 were both positive or both negative (i.e. if their signs matched.) If what I believe is correct, you should look at the SIGN function. Even if you modify this to use SIGN and do what you originally wanted, wouldn't you want to check if the _values of the function at the two points_ have the same signs, not if the _two points_ have the same signs? > if x2/x>1 > x2=x+dx; > x=x2; > else > x=x-(x/2); > end > end > end > break > end > > fun is a function containing my function. The if statements decide whether > or not to switch directions and step size based on the ratio between two > consecutive functions as follows: > If ratio of consecutive values (and both have same sign), > f (xk )/f (xk −1 ), is greater then 1 then switch direction of > search 􏰀 If ratio of consecutive values (and both have same sign) > is less that > 1, then go the same direction with same step size > 􏰀 If consecutive values are of opposite sign then switch > directions and reduce the step size I think you're getting confused with which variables to modify. The two main changes I would suggest you make in your code: 1) Write the algorithm in your function as lines of comments. This will allow you to insert the code for each step immediately after the comment explaining that step, and may help you (and those you ask to help) more easily understand what each particular step is doing. 2) Short variable names are easy to type, but longer variable names are more descriptive. Rather than using variables x, x2, and y I would use as names something like xAtIterationK and xAtIterationK_1 for x_k and x_(k-1). This makes it clearer exactly how each variable fits into the algorithm. If you choose your variable names well, you might even be able to copy your algorithm comments and make one or two changes to convert them directly into the code. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
|
Pages: 1 Prev: memory restriction? Next: Schwarz Christoffel Help |