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:59 Nathan <ngreco32(a)gmail.com> wrote in message <bfd0dfb9-6aca-45d7-82b8-6b33ad89f1d8(a)j36g2000prj.googlegroups.com>... > 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 Wow, thank you, Nathan! I'm going over your comments now. Matlab is a little overwhelming, so please forgive my noobness. But your comments make sense, and is what I was beginning to suspect. I've been changing lines to different things to see what changes happen and it's helping me decipher Matlab a little better. I'm sure I'll have more questions, but now this helps. Thanks! :) Francis
From: Nathan on 14 May 2010 19:13 On May 14, 3:54 pm, "Francis Fernandez" <check...(a)yahoo.com> wrote: > "x" and "err" are being assigned the values of x and error_flag? That is correct. Also, to "cout" to the matlab command window, you can either use "disp(some_string)" or leave the semi-colon off of an assignment operation. Type these operation in your matlab command window: A = 1:4; %should be no output A = 1:4 %you should see 1 2 3 4 in your command window A; %should be no output A %you should see 1 2 3 4 in your command window disp(A) %again, 1 2 3 4 disp(A); %this will display 1 2 3 4, despite the semi colon Note you can also use the SPRINTF command and FPRINTF commands to display strings. -Nathan
From: Steven Lord on 17 May 2010 11:01
"Francis Fernandez" <check013(a)yahoo.com> wrote in message news:hskkjp$doq$1(a)fred.mathworks.com... > Nathan <ngreco32(a)gmail.com> wrote in message > <bfd0dfb9-6aca-45d7-82b8-6b33ad89f1d8(a)j36g2000prj.googlegroups.com>... *snip* > Wow, thank you, Nathan! I'm going over your comments now. Matlab is a > little overwhelming, so please forgive my noobness. But your comments > make sense, and is what I was beginning to suspect. I've been changing > lines to different things to see what changes happen and it's helping me > decipher Matlab a little better. I'm sure I'll have more questions, but > now this helps. Thanks! :) I second dpb's suggestion to you to work your way through the Getting Started section of the documentation. That explains some of the basic background information about MATLAB syntax that it sounds like you haven't been taught. You might also find running the code using the Debugger to be useful, as it will allow you to step through the code and see what it does each line along the way. http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/brqxeeu-175.html -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ |