07-23-2003, 11:42 AM | #1 (permalink) |
Psycho
Location: cali
|
Matlab
anyone here good with Matlab? i'm just fielding for answers right now because I have a Matlab project due this coming Monday, July 28, 2003 and can not get help.
and for those of you that have a firm grasp of Matlab, will any of you be checking this forum frequently during the duration of the weekend? thanks in advance |
07-23-2003, 12:23 PM | #2 (permalink) |
Upright
Location: Philadelphia
|
depends what your doing with it. Ive done some dynamic systems modeling with it, and i have been known to make simulink my bitch in the past. post some more details.
__________________
You dont get this fat from eating fucking ramen. |
07-23-2003, 09:09 PM | #4 (permalink) |
Psycho
Location: cali
|
thanks guys.
well, they're basic. it's to solve a math problem of positioning. my problem is the organization part of it and how to translate the algorithm into code. i'll post the questions in a bit. thanks again guys.
__________________
no man or woman is worth your tears - and the one who is, won't make you cry question authority, don't ask why, just do it! |
07-24-2003, 07:55 AM | #8 (permalink) |
Psycho
Location: cali
|
you guys rock!
ok, the question is this: Tom the cat is running after Jerry the mouse. Tom's velocity is a constant c. Jerry's velocity depends on how close Tom is from Jerry: v(t) = vm - k * S(t), where vm is Jerry's maximum velocity, k is proportional constant, t is time, S(t) is the distance between Tom and Jerry at time t. The initial distance betweeen Tom and Jerry at t = 0 is S0. We found that the time T when Tom will catch Jerrr satisfies the following equation: F(T) = [(vm - c)/k] * (1 - e^-kt) + S0e^-kt = 0 Suppose S0 = 100m, c = 4 m/s, vm = 3 m/s, k = 0.02s^-1. Use the bisection method, Newton's method, secant method and method of false position to solve for T. For Newton's method, use initial guess T0 = 300 s. For the other 3 methods, search in the interval [0, 300]. For all the methods, use the stopping criteria: |Tn - Tn - 1 |/|Tn | < 10^-4 How many iterations does each method need? Solve F(T) = 0 by hand to get the true solution and compute the relative error for the numerical result produced by each method. ok, that's the problem. i have the algorithms for each of the above mentioned methods, but i can't seem to initialize anything or get any of my functions to work properly. maybe something to do with my .m files. thanks again guys. if you guys need, i'll post the algorithms to the above mentioned methods. thanks. |
07-24-2003, 08:09 AM | #9 (permalink) |
Crazy
Location: Oxford, UK
|
Okay - I'm having a look.
Firstly, it's probably helpful to express your equation like this... Code:
function time = T (t) % constants S0 = 100; % m c = 4; % m/s vm = 3; % m/s k = 0.02; % s^-1. time = ((vm - c)/k) * (1 - exp(-k*t)) + S0*exp(-k*t); Code:
times = 0:0.1:300; plot (times, T(times)) Could you post the algorithms? I don't know what they mean. Last edited by Taliesin; 07-24-2003 at 08:16 AM.. |
07-24-2003, 05:53 PM | #11 (permalink) |
Psycho
Location: cali
|
sorry it took so long, i had a quiz and for those interested in how well i did, i did awesome on it. well, here are the algorithms:
Bisection Method: INPUT: endpoints a, b; tolerance TOL; maximum number of iterations N0 . OUTPUT: approximate solution p or message of failure. Step 1: Set i = 1; FA = f(a). Step 2: While i <= N0 do Steps 3 - 6 Step 3: set p = a + (b-a)/2; (Compute pi .) FP = f(p). Step 4: If FP = 0 or (b-a)/2 < TOL then OUTPUT (p); (Procedure completed successfully.) STOP. Step 5: Set i = i + 1. Step 6: If FA * FP > 0 then set a = p; (Compute ai , bi .) FA = FP else set b = p. Step 7: OUTPUT ('Method failed after N0 iterations, N0 =', N0); (The procedure was unsuccessful.) STOP. Newton's Method: INPUT: initial approximation p0; tolerance TOL; maximum number of iterations N0 . OUTPUT: approximate solution p or message of failure. Step 1: Set i = 1; Step 2: While i <= N0 do Steps 3 - 6 Step 3: Set p = p0 - f(p0) / f'(p0). (Compute pi.) Step 4: If | p - p0| < TOL then OUTPUT (p); (The procedure was successful.) STOP. Step 5: Set i = i + 1. Step 6: Set p0 = p; (Update p0.) Step 7: OUTPUT ('Method failed after N0 iterations, N0 =', N0); (The procedure was unsuccessful.) ** i will add the other two, this one is getting too long, plus it'll give me a way to up my post count ** |
07-24-2003, 06:58 PM | #12 (permalink) |
Riiiiight........
|
hmmm.. are we supposed to solve this for you??
borrow a introductory book about MATLAB from the library. Will teach you waaaay more about functions than we can from the board. TIP: if you wish to do linear multiplication/division, instead of matrix multiplication, use ./ , .* instead of / and * |
07-24-2003, 08:04 PM | #13 (permalink) |
Psycho
Location: cali
|
i was not aimin to have it solved for me. as stated earlier, i had problems initializing and organizing the .m files.
__________________
no man or woman is worth your tears - and the one who is, won't make you cry question authority, don't ask why, just do it! |
07-24-2003, 08:41 PM | #14 (permalink) |
Tilted
Location: Oregon
|
If you are still having problems try asking specificly questions like "I am trying to do such and such. Here is what I did... Here is what it told me...What should I do differently?" Maybe that way we can help you out without just doing it for you.
|
07-24-2003, 09:21 PM | #15 (permalink) | ||
Psycho
Location: cali
|
oh, i was just answering the questions to the above first....
Quote:
Quote:
__________________
no man or woman is worth your tears - and the one who is, won't make you cry question authority, don't ask why, just do it! |
||
07-24-2003, 09:23 PM | #16 (permalink) |
Psycho
Location: cali
|
basically, for the above mentioned problem i don't know how many .m files would be ideal. to me it seems as though each method mentioned would have (1) with the algorithm on it in code calling another .m file with the function on it. however in class, some girl turned in what looked like a 30 page package and she writes small.
__________________
no man or woman is worth your tears - and the one who is, won't make you cry question authority, don't ask why, just do it! |
07-24-2003, 09:28 PM | #17 (permalink) |
Psycho
Location: cali
|
the last 2 algorithms are:
Secant Method: INPUT: initial approximations p0, p1; tolerance TOL; maximum number of iterations N0. OUTPUT: approximate solution p or message of failure Step 1: Set i = 2; q0 = f(p0); q1 = f(p1); Step 2: While i <= N0 do steps 3 – 6 Step 3: Set p = p1 – q1(p1 – p0)/(q1 – q0). (Compute pi). Step 4: If |p – p1| < TOL then OUTPUT (p); (The procedure was successful.) STOP Step 5: Set i = i + 1. Step 6: Set p0 = p1; (Update p0, q0, p1, q1.) q0 = q1; p1 = p; q1 = f(p) Step 7: OUTPUT (‘The method failed after N0 iterations, N0 =’, N0); (The procedure was unsuccessful.) STOP. Method of False Position: INPUT: initial approximations p0, p1; tolerance TOL; maximum number of iterations N0. OUTPUT: approximate solution p or message of failure Step 1: Set i = 2; q0 = f(p0); q1 = f(p1); Step 2: While i <= N0 do steps 3 – 7 Step 3: Set p = p1 – q1(p1 – p0)/(q1 – q0). (Compute pi). Step 4: If |p – p1| < TOL then OUTPUT (p); (The procedure was successful.) STOP Step 5: Set i = i + 1. q = f(p). Step 6: If q * q1 < 0 then set p0 = p1; q0 = q1; Step 7: Set p1 = p; q1 = q. Step 8: OUTPUT (‘The method failed after N0 iterations, N0 =’, N0); (The procedure was unsuccessful.) STOP. |
07-24-2003, 11:36 PM | #18 (permalink) |
Tilted
Location: Oregon
|
I think I would go with 4 .m files as well. One for secant, newton, bisect, and false position.
In anything relating to code, don't worry to much about what other people are doing. I know it makes you a little nervous when you see someone doing something completely different than the way you are doing it. Just remember, especially with code, there are a ton of different ways to do everything. Just choose a way that makes sense to you. |
07-25-2003, 08:21 AM | #19 (permalink) |
Psycho
Location: cali
|
ok, i had this in mind as well. since all of the last steps call for an output, can i specify one in a separate .m file. i thought that i may, but i remember when calling a function from an .m file, they had to have relatively the same name. is this true?
__________________
no man or woman is worth your tears - and the one who is, won't make you cry question authority, don't ask why, just do it! |
07-25-2003, 08:34 AM | #20 (permalink) |
Riiiiight........
|
seems that all of them have different inputs, so you should do it in 4 different .m files.
subfunctions within a function .m file can only be called within the .m file, so if you want to write a function that can be called by other functions, write it as a separate .m file. to output things from a .m file function [variable1, variable 2] = filename(input1, input2) where inputXX is your inputs, filename is the name of your function, and variableXX is what you wish to output. |
07-25-2003, 08:40 AM | #21 (permalink) |
Riiiiight........
|
check this place out....
http://www.mathworks.com/access/help...c/matlab.shtml its a pretty decent place. I picked up MATLAB on-the-fly pretty much by using the online help, and a friend's textbook.... pretty hairy for a while, considering i wasn't a CS student, and my heuristics class was filled with CS grad students...... |
07-25-2003, 03:06 PM | #23 (permalink) |
Psycho
Location: cali
|
another thing i'm having problems understanding is that in the algorithm for bisection method, i'm supposed to (at Step 1) FA = f(a). but no where does it state what the function f is. or am i reading that backwards?
also, others in class have hard coded what the variables a, b, TOL and N0 are for each, but i would like to prompt the user for input it would go like this right? a = input('Please enter starting endpoint a for the Bisection Method')
__________________
no man or woman is worth your tears - and the one who is, won't make you cry question authority, don't ask why, just do it! Last edited by slant eyes; 07-25-2003 at 03:09 PM.. |
07-25-2003, 03:28 PM | #24 (permalink) |
Riiiiight........
|
hasn't f already been given??
F(T) = [(vm - c)/k] * (1 - e^-kt) + S0e^-kt = 0 Advice: If you're in college, you're probably paying very good money to the school for your education. MAKE USE OF IT. I typically find that one hour spend during office hours, is worth 4 hours bashing my head against the wall. I think it should be right. why don't you try it out in the command window or in a new matlab file?? |
07-25-2003, 03:44 PM | #25 (permalink) |
Psycho
Location: cali
|
i've gone to office hours, and tried that whole thing, however the professor refuses to give us any advice on this computer project because the TA is grading it. and to top it off, the TA doesn't have office hours, says she's not obligated to have any because it is summer. adding insult to injury, i can hardly understand the both of them because of their heavy chinese accents, AND I'M CHINESE!
well, i coded one of the algorithms, i'll post it here and if you guys can let me know if i have something wrong. i think i'm doing the disp(sprintf('s wrong.
__________________
no man or woman is worth your tears - and the one who is, won't make you cry question authority, don't ask why, just do it! |
07-25-2003, 03:48 PM | #26 (permalink) |
Psycho
Location: cali
|
ok, here is one algorithm. forgive me if it's newb-ish.
Code:
a = 0; b = 0; TOL = 0; N0 = 0; p = 0; i = 1; a = input('Please enter starting endpoint "a" for the Bisection Method'); b = input('Please enter starting endpoint "b" for the Bisection Method'); TOL = input('Please enter desired tolerance for the Bisection Method'); N0 = input('Please enter maximum number of iterations for the Bisection Method'); FA = f(a); for i = i:N0 p = a + (b - a) / 2; FP = f(p); if FP = 0 | (b - a) / 2 < TOL; disp(sprintf('%5.6f', p); end else i = i + 1; if FA * FP > 0; a = p; FA = FP; else b = p; disp('Method failed after N0 iterations, N0 =', N0); end |
07-25-2003, 05:36 PM | #27 (permalink) | |
Riiiiight........
|
Quote:
Use a Boolean variable to keep track of whether the SUCCESS conditions have been met. Terminate the loop when you have succesfully done it. probably easier to do in in a WHILE loop in this case. Store the result as a variable, don't print it out, and make this function output that variable. I normally code the inputs into the function itself, so that its easy rerunning the function in the command window if it doesnt work. you just press the 'up' button. |
|
07-25-2003, 09:43 PM | #28 (permalink) | |||
Psycho
Location: cali
|
so you're saying i should hard code it? or just for the sake of testing purposes.
Code:
%Bisection Method a = 0; b = 0; TOL = 0; N0 = 0; p = 0; i = 1; a = input('Please enter starting endpoint "a" for the Bisection Method'); b = input('Please enter starting endpoint "b" for the Bisection Method'); TOL = input('Please enter desired tolerance for the Bisection Method'); N0 = input('Please enter maximum number of iterations for the Bisection Method'); FA = f(a); while i <= N0 p = a + (b - a) / 2; FP = f(p); if FP = 0 | (b - a) / 2 < TOL; disp(sprintf('%5.6f', p); else i = i + 1; if FA * FP > 0; a = p; FA = FP; else b = p; end end end disp('Method failed after N0 iterations, N0 =', N0); Quote:
Quote:
Code:
if FP = 0 | (b - a) / 2 < TOL; disp(sprintf('%5.6f', p); end Quote:
thanks for your help. oh yeah, does the last function that displays the message using the disp(sprintf make sense? is it wrong? |
|||
07-26-2003, 10:01 AM | #29 (permalink) |
Riiiiight........
|
so does it work? if it does... then good...
but i can see your code printing out an infinite amount of disp(sprintf('%5.6f', p); if it does find the solution in less than NO iterations.... how about function answer = bisect(a,b,TOL,NO); answerfound = 0; answer = 0; while (i<NO)&(answerfound=0)...... and if FP = 0 | (b - a) / 2 < TOL; answer = p; answerfound = 1; end http://www.mathworks.com/access/help...9.shtml#694422 |
07-27-2003, 07:52 AM | #31 (permalink) |
Riiiiight........
|
RTFM!!!
read the manual...... its really hard explaining some of these things over the internet. Have you borrowed a book from the library about MATLAB yet? its MOST likely that it will explain this MUCH better than me.... have you read the online documentation yet? the link i sent describes it quite well. read the surrounding stuff too..... |
07-28-2003, 10:15 AM | #32 (permalink) |
Psycho
Location: cali
|
ok guys, i thought you all might want an update. after a night of about 3 hours of sleep, many printouts to bring with me to work, hundreds of scribbles, i've finally got them to work, and work correctly. and i swear, no academic dishonesty at all. all on my own, with the help of you fine people, especially you dimbulb.
does anyone want to see the final code?
__________________
no man or woman is worth your tears - and the one who is, won't make you cry question authority, don't ask why, just do it! |
07-28-2003, 11:43 AM | #35 (permalink) |
Psycho
Location: cali
|
here's the bisection, i changed it a bit since the last time.
Code:
N = 100; p = 0; i = 1; s0 = 100; c = 4; vm = 3; k = 0.02; a = input('Please enter starting endpoint "a" for the Bisection Method '); b = input('Please enter starting endpoint "b" for the Bisection Method '); Fa = (vm - c) / k * (1 - exp(-k*a)) + s0*exp(-k*a); FA = Fa; FP = 0; while i <= N p1 = p; p = a + (b - a) / 2; FP = (vm - c) / k * (1 - exp(-k*p)) + s0*exp(-k*p); if abs(p - p1) / abs(p) < 10^-4; break; else i = i + 1; if FA * FP > 0; a = p; FA = FP; else b = p; end end end disp(sprintf('The Bisection Method: %5.10f', p)); disp(sprintf('Total Number of Iterations = %1.1f', i)); Code:
N = 200; p0 = 0; TOL = 0; N0 = 0; p = 0; i = 1; s0 = 100; c = 4; vm = 3; k = 0.02; p0 = input('Please enter initial approximation "p0" for Newtons Method '); while i <= N; Fp0 = ((vm - c) / k) * (1 - exp(-k*p0)) + s0*exp(-k*p0); q0 = ((vm - c) / k) * (k * exp(-k*p0)) - s0*k*exp(-k*p0); p1 = p; p = p0 - Fp0 / q0; if abs(p - p1) / abs(p) < 10^-4 break; else i = i + 1; p0 = p; end end disp(sprintf('Newtons Method: %5.10f', p)); disp(sprintf('Total Number of Iterations = %1.1f', i)); Code:
N = 200; p0 = 0; p1 = 0; TOL = 0; N0 = 0; p = 0; i = 2; s0 = 100; c = 4; vm = 3; k = 0.02; p0 = input('Please enter initial approximation "p0" for the Secant Method '); p1 = input('Please enter initial approximation "p1" for the Secant Method '); while i <= N q0 = (vm - c) / k * (1 - exp(-k*p0)) + s0*exp(-k*p0); q1 = (vm - c) / k * (1 - exp(-k*p1)) + s0*exp(-k*p1); r = p; p = p1 - q1 * (p1 - p0) / (q1 - q0); if abs(p - r) / abs(p) < 10^-4 break; else i = i + 1; p0 = p1; p1 = p; end end disp(sprintf('The Secant Method: %5.10f', p)); disp(sprintf('Total Number of Iterations = %1.1f', i)); Code:
N = 200; p0 = 0; p1 = 0; p = 0; i = 2; s0 = 100; c = 4; vm = 3; k = 0.02; p0 = input('Please enter initial approximation "p0" for the Method of False Position '); p1 = input('Please enter initial approximation "p1" for the Method of False Position '); while i <= N q0 = (vm - c) / k * (1 - exp(-k*p0)) + s0*exp(-k*p0); q1 = (vm - c) / k * (1 - exp(-k*p1)) + s0*exp(-k*p1); FP = 0; r = p; p = p1 - q1 * (p1 - p0) / (q1 - q0); FP1 = FP; if abs(p - r) / abs(p) < 10^-4 break; else i = i + 1; q = (vm - c) / k * (1 - exp(-k*p)) + s0*exp(-k*p); end if q * q1 < 0 p0 = p1; q0 = q1; else p1 = p; q1 = q; end end disp(sprintf('The Method of False Position: %5.10f', p)); disp(sprintf('Total Number of Iterations = %1.1f', i)); |
07-29-2003, 12:05 AM | #37 (permalink) |
Crazy
Location: Oxford, UK
|
All looks fine to me.
One nifty way to improve this might be to have the user select the method, and to use a switch to select the correct function call. Also, when I'm dictator I'm going to force Mathworks to implement a unary increment/decrement operator like C's ++/--. |
07-29-2003, 07:29 AM | #38 (permalink) |
Psycho
Location: cali
|
i thought about using the switch case for all this, however the professor was not going to run each of these programs. he just wanted the printouts and the code. hence is the reason i didn't implement a main function with a switch/case statement to call each of these.
and about the increment/decrement, i couldn't agree more. thanks
__________________
no man or woman is worth your tears - and the one who is, won't make you cry question authority, don't ask why, just do it! |
07-29-2003, 08:02 AM | #39 (permalink) |
Insane
|
Seems you are doing fine.. Here are some tips:
You can use ctrl-I in the editor to auto indent your code. IMO, fprintf is nicer than disp(sprintf.. syntax is the same, just add \n for newline. I would also skip the input(-statements.. saves the hassle of typing in all the constants at run time. -gal |
Tags |
matlab |
|
|