Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Knowledge and How-To (https://thetfp.com/tfp/tilted-knowledge-how/)
-   -   Matlab (https://thetfp.com/tfp/tilted-knowledge-how/18361-matlab.html)

slant eyes 07-23-2003 11:42 AM

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

phatman 07-23-2003 12:23 PM

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.

cheerios 07-23-2003 03:55 PM

I have used it, but it's been a WAY long fuckin' time. you may be better off googling "Matlab" or something.

but hell, shoot off those questions, we'll see what we can do. :)

slant eyes 07-23-2003 09:09 PM

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.

badflsh 07-23-2003 10:40 PM

I should be checking in this weekend as well and should be able to help with basic stuff.

Taliesin 07-24-2003 03:58 AM

I have Matlab currently open in another window. What do you need?

Peetster 07-24-2003 04:06 AM

I've used Rlab, which is a Linux Matlab clone sortof. Mainly done fortran style work on it. If I see a question that I can help with, I'll post!

slant eyes 07-24-2003 07:55 AM

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.

Taliesin 07-24-2003 08:09 AM

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);

... which means you can do cool stuff like ...

Code:

times = 0:0.1:300;

plot (times, T(times))

... which (I think) is the time-to-contact for a series of times.

Could you post the algorithms? I don't know what they mean.

Taliesin 07-24-2003 08:34 AM

Wait a second - the function I just gave returns both positive and negative values. Something must be wrong there. I'll have a look again...

slant eyes 07-24-2003 05:53 PM

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 :D **

dimbulb 07-24-2003 06:58 PM

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 *

slant eyes 07-24-2003 08:04 PM

i was not aimin to have it solved for me. as stated earlier, i had problems initializing and organizing the .m files.

badflsh 07-24-2003 08:41 PM

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.

slant eyes 07-24-2003 09:21 PM

oh, i was just answering the questions to the above first....
Quote:

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
Quote:

Could you post the algorithms?

slant eyes 07-24-2003 09:23 PM

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.

slant eyes 07-24-2003 09:28 PM

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.

badflsh 07-24-2003 11:36 PM

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.

slant eyes 07-25-2003 08:21 AM

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?

dimbulb 07-25-2003 08:34 AM

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.

dimbulb 07-25-2003 08:40 AM

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......

slant eyes 07-25-2003 08:50 AM

linky bookmarked...thanks

slant eyes 07-25-2003 03:06 PM

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')

dimbulb 07-25-2003 03:28 PM

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??

slant eyes 07-25-2003 03:44 PM

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.

slant eyes 07-25-2003 03:48 PM

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


dimbulb 07-25-2003 05:36 PM

Quote:

Originally posted by slant eyes
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


for one... you should only say that the method has failed AFTER you are done with the loop. check your logic. I'm not going to do that for you.

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.

slant eyes 07-25-2003 09:43 PM

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:

for one... you should only say that the method has failed AFTER you are done with the loop. check your logic. I'm not going to do that for you.
i changed it, thanks for pointing that out for me. does that make sense now?

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.
there is a boolean checking...

Code:

if FP = 0 | (b - a) / 2 < TOL;
    disp(sprintf('%5.6f', p);
end

Quote:

Store the result as a variable, don't print it out, and make this function output that variable
i thought i was storing it as a variable p, which i do print with the disp(sprintf function...

thanks for your help. oh yeah, does the last function that displays the message using the disp(sprintf make sense? is it wrong?

dimbulb 07-26-2003 10:01 AM

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

slant eyes 07-26-2003 08:42 PM

how do i incorporate the function f into the .m file? i don't understand it. thanks

dimbulb 07-27-2003 07:52 AM

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.....

slant eyes 07-28-2003 10:15 AM

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?

Taliesin 07-28-2003 10:35 AM

I would.

Sorry I didn't help more - I forgot about the thread!

dimbulb 07-28-2003 11:24 AM

woohoo!!

code please!!

slant eyes 07-28-2003 11:43 AM

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));

here's the newton's method

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));

here's the secant method:

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));

and finally, here's the method of false position

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));

oh yeah, forgive me for the lack of comments. i know it's bad, but i was tired:p

slant eyes 07-28-2003 10:30 PM

any comments? or suggestions?

Taliesin 07-29-2003 12:05 AM

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 ++/--.

slant eyes 07-29-2003 07:29 AM

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

gal 07-29-2003 08:02 AM

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

Taliesin 07-29-2003 09:32 AM

Hah - ctrl-A ctrl-I is my most used key combo!


All times are GMT -8. The time now is 10:39 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360