Results 1 to 10 of 16
Thread: White needs help
-
- Join Date
- 11-13-07
- Location
- Plano, TX and Ruston, LA
- Posts
- 32,364
- Post Thanks / Like
- Blog Entries
- 43
12-03-10, 03:45 PM #1White needs help
So, I'm supposed to be writing a piece of MATLAB code to approximate cos to 8 sig figs. Seems easy right? It seems easy to me, but for some reason, I can't figure out why my while loop won't stop after 8 sig figs are reached. Any help/hints would be appreciated. Keep in mind that this is the first coding I've done in about 2 years.
Code:clear x=.3*pi; n=2; %number of sig figs es=0.5*10^(2-n); %target error et=1; %actual error trueVal=cos(x); term(1)=1; counter=2; while (abs(et)>=es) counter1=(counter-1)*2; term(counter)=((-1)^(counter-1))*(x^counter1)/factorial(counter1); total=sum(term); et=((trueVal-total)/trueVal)*100; counter=counter+1; end
enf-Jesus its been like 12 minutes and you're already worried about stats?! :-P
Bigdog-Sweet home Alabama you are an idiot.
-
- Join Date
- 11-13-07
- Location
- Plano, TX and Ruston, LA
- Posts
- 32,364
- Post Thanks / Like
- Blog Entries
- 43
12-03-10, 03:49 PM #2Re: White needs help
I also have tried to meet with the professor, but he's in a meeting right now and only meets with students by appointment. The project is due on Monday so I've got some time.
enf-Jesus its been like 12 minutes and you're already worried about stats?! :-P
Bigdog-Sweet home Alabama you are an idiot.
-
- Join Date
- 11-13-07
- Location
- Plano, TX and Ruston, LA
- Posts
- 32,364
- Post Thanks / Like
- Blog Entries
- 43
12-03-10, 03:52 PM #3Re: White needs help
I also set the number of sig figs low in the above to see if it was just a long run time, but from removing the semi-colon on the counter var above, I've seen that the code is running into very high iterations.
enf-Jesus its been like 12 minutes and you're already worried about stats?! :-P
Bigdog-Sweet home Alabama you are an idiot.
-
-
- Join Date
- 11-13-07
- Location
- Plano, TX and Ruston, LA
- Posts
- 32,364
- Post Thanks / Like
- Blog Entries
- 43
-
- Join Date
- 05-28-07
- Location
- East Texas
- Posts
- 7,960
- Post Thanks / Like
- Blog Entries
- 9
12-03-10, 04:10 PM #6Re: White needs help
Repost From Scripting About Sig Fig Functions | TheDailyReviewer
http://mathfax.com/search/do_sig_figs
I can't help but maybe google can.Last edited by deputyfestus; 12-03-10 at 04:11 PM.
-
12-03-10, 05:10 PM #7
Re: White needs help
I'm not entirely sure what problem your having... I ran your code with n=8 and got the following results.
trueVal =
0.587785252292473
>> total
total =
0.587785251272005
>> counter
counter =
7
Which is accurate to 8 sig figs and only took 7-2=5 iterations. How many iterations are you getting?
-
- Join Date
- 11-13-07
- Location
- Plano, TX and Ruston, LA
- Posts
- 32,364
- Post Thanks / Like
- Blog Entries
- 43
12-03-10, 05:16 PM #8Re: White needs help
I have to shut down MatLab using Process Explorer to get it to stop running. I've seen it running as high as 180k iterations. I can't figure out what's up.
enf-Jesus its been like 12 minutes and you're already worried about stats?! :-P
Bigdog-Sweet home Alabama you are an idiot.
-
12-03-10, 05:37 PM #9
Re: White needs help
Damn man... I'm not sure what that could be. Like I said, I ran it and it worked just fine.
One troubleshooting technique you could try is to insert a break point at the beginning of your while loop. Then just run your script one step at a time and make sure your getting the values you expect for term/total/et during the first couple of iterations.
-
- Join Date
- 11-13-07
- Location
- Plano, TX and Ruston, LA
- Posts
- 32,364
- Post Thanks / Like
- Blog Entries
- 43
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks