Prev: Returning a specified iteration from iterative solvers such as bicgstab
Next: Parallel port access without Data Acquisition Toolkit
From: Francis Fernandez on 14 May 2010 18:05 Hi. I'm currently taking a summer class which requires Matlab and is going at the speed of light, unfortunately. Now I have no experience with Matlab whatsoever. I'm not sure on where even to begin. My questions are dumb, I know, and I'm growing desperate fast. I'm looking at an example that the professor gave us in class and I'm trying to decipher what does it all mean, since he left no notes on the lines. This is the function: function main a = -2; b = 0; eps = 1e-7; [x,err]=mybisection( a, b, eps) end function y=f(x) y = exp( -(x^2)/2 ) - x * cos(x); end function [x, error_flag] = mybisection( a, b, eps ) fa = f(a); fb = f(b); if fa * fb > 0 error_flag = 0; x = 0; else while abs( b-a ) > eps c = (a + b) / 2.0; fc = f(c); if fa * fc <= 0.0 b = c; fb = fc; else a = c; fa = fc; end end x = (a + b) / 2.0; error_flag = 1; end end Can anyone help me decipher what these lines are doing? "[x,err]=mybisection( a, b, eps)" It looks like it creates an array. But I only see "err" one time in the entire program. So how does Matlab know how big is this matrix? Also, I don't see the function "mybisection(a, b, eps)" defined anywhere. The program runs just as the professor said it would. I just have no idea why. Can someone help me understand this step by step? I am a mechanical engineering student, but programming isn't my forté. So I apologize in advance if these questions are inappropriate, offensive, or prohibited in these forums. Thanks! Francis
From: dpb on 14 May 2010 18:20 Francis Fernandez wrote: .... > I'm looking at an example that the professor gave us in class and I'm > trying to decipher what does it all mean, since he left no notes on the > lines. This is the function: > > function main > > a = -2; b = 0; eps = 1e-7; > [x,err]=mybisection( a, b, eps) > ....snip... > function [x, error_flag] = mybisection( a, b, eps ) .... > Can anyone help me decipher what these lines are doing? > > "[x,err]=mybisection( a, b, eps)" > It looks like it creates an array. What does this line do??? That change your thinking any? > function [x, error_flag] = mybisection( a, b, eps ) .... > I don't see the function "mybisection(a, b, eps)" defined anywhere. You don't??? Look again... Read "Getting Started" section in online help about "Programming with Matlab" and "Scripts and Functions" > The program runs just as the professor said it would. I just have no > idea why. Can someone help me understand this step by step? .... Take a little more time first and work your way thru it more carefully... --
From: Matt Fig on 14 May 2010 18:31 The function mybisection IS defined. This should give you a clue as to what it is doing. Add these two lines to the main function, after the call to mybisection: X = x-10:.1:x+10; plot(X,f(X),'b',x,f(x),'*r') and change the function f to this: y = exp( -(x.^2)/2 ) - x .* cos(x); Then re-run the code. What is special about the location of the red dot?
From: Nathan on 14 May 2010 18:37 I will separate functions by a line of ~'s. function main %main function a = -2; %give a the value of -2 b = 0; %give b the value of 0 eps = 1e-7; %give eps the value of .0000001 [x,err]=mybisection( a, b, eps) %call the function "mybisection" with a,b, and eps %as input arguments, returning x and err as output %arguments. end ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ function y=f(x) %f function with input x and output y y = exp( -(x^2)/2 ) - x * cos(x); end ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ function [x, error_flag] = mybisection( a, b, eps ) %mybisection function with inputs a,b,eps and %outputs x,error_flag fa = f(a); %call function f with input a fb = f(b); %call function f with input b if fa * fb > 0 %logic check error_flag = 0; %set error to zero x = 0; %set x to zero else while abs( b-a ) > eps %more logic check: %checks if a and b are essentially equal %within eps c = (a + b) / 2.0; fc = f(c); %call function f with input c if fa * fc <= 0.0 %more logic check b = c; fb = fc; else a = c; fa = fc; end end x = (a + b) / 2.0; error_flag = 1; %error set to 1 end end Is that enough commenting to help sort it out for you? Whenever you see the word "function", that should be the beginning of a function. Sometimes "functions" are shown to be finished with an "end" statement, as you can see in this code. Your program starts in the function "main", calls function "mybisection". Inside of "mybisection", the function "f" is called multiple times. The outputs of the function "mybisection" are placed in x and err, respectively. Any other questions? -Nathan
From: Francis Fernandez on 14 May 2010 18:54
"Matt Fig" <spamanon(a)yahoo.com> wrote in message <hskiva$spd$1(a)fred.mathworks.com>... > The function mybisection IS defined. This should give you a clue as to what it is doing. Add these two lines to the main function, after the call to mybisection: > > X = x-10:.1:x+10; > plot(X,f(X),'b',x,f(x),'*r') > > and change the function f to this: > > y = exp( -(x.^2)/2 ) - x .* cos(x); > > > Then re-run the code. What is special about the location of the red dot? Ok. I see where it is defined now. The red dot is my zero (y=0), which is what it's supposed to find given a range [a,b] and within a defined margin of error. Thanks. That helped. Also, I've been changing things here and there, and looking to see what errors these changes produce to understand Matlab's language a little better. And it's helping (LOL). Also, what was throwing me off a bit is that there is no "cout" or "cin" like in c++. Am I correct to assume that the line "[x,err]=mybisection(a,b,eps)" is causing the outputs of "x" and "err" from the main, giving it the values from the function "function [x, error_flag] = mybisection(a, b, eps)"? "x" and "err" are being assigned the values of x and error_flag? |