Ask your own question, for FREE!
Mathematics 14 Online
OpenStudy (raffle_snaffle):

matlab help please

OpenStudy (raffle_snaffle):

%% Problem 9.14 %{ The value of cos(x) can be approximated using a Maclaruin series cos(x) = 1 - x^2/2! + x^4/4! + x^6/6! + ..... which can be expressed more compacity as Equation in the textbook (recall that the symbol ! stans for factorial). Use a midpint break loop to determine how many terms must be included in the summation, in order to find the coreect value of cos(2) within an error of 0.001. Limit the number of iterations to a maximums of 10. %}

OpenStudy (raffle_snaffle):

@phi

OpenStudy (raffle_snaffle):

Okay I agree with the statement. I am just thinking

OpenStudy (raffle_snaffle):

so the equation I will be using in my for loop is the one shown in the textbook... let me find it

OpenStudy (raffle_snaffle):

y(k) = (((-1)^(k-1)) * ((x^((k-1)*2)) / factorial((k-1)*2));

OpenStudy (phi):

I think we can stop when the absolute value of the term is < 0.001

OpenStudy (raffle_snaffle):

% defining variables c = 0.001; max_iter = 10; x = 2; % initiating values for the summation y(1) = 1; total(1) = y(1); % values for the machlaurin series for k = 2:max_iter y(k) = (((-1)^(k-1)) * ((x^((k-1)*2)) / factorial((k-1)*2));

OpenStudy (raffle_snaffle):

Do I need to use a break?

OpenStudy (phi):

yes, use a break

OpenStudy (raffle_snaffle):

% defining variables c = 0.001; max_iter = 10; x = 2; % initiating values for the summation y(1) = 1; total(1) = y(1); % values for the machlaurin series for k = 2:max_iter y(k) = (((-1)^(k-1)) * ((x^((k-1)*2)) / factorial((k-1)*2)); total(k) = total(k-1) + y(k);

OpenStudy (raffle_snaffle):

where do i use the break?

OpenStudy (phi):

Here's what I have x= 2.0; val= 0; max_iter= 10; for nn=0: max_iter-1 n= 2*nn; tt= x^n / factorial(n); val= val+ (-1)^nn * tt if tt<0.001 break; end; end; I think if the term is < 0.001 we don't need to use it. But it seems dumb to calculate it and not use it in the sum.

OpenStudy (raffle_snaffle):

brb

OpenStudy (raffle_snaffle):

@jim_thompson5910

OpenStudy (raffle_snaffle):

Mathematically I am not exactly sure what is going on here.

OpenStudy (raffle_snaffle):

Its been awhile since I have taken calc 3. lol

OpenStudy (raffle_snaffle):

equation in the textbook is this y(k) = (((-1)^(k-1)) * ((x^((k-1)*2)) / factorial((k-1)*2))

jimthompson5910 (jim_thompson5910):

tbh, I've never heard of a "midpoint break loop" what is it exactly?

OpenStudy (raffle_snaffle):

https://www.youtube.com/watch?v=fuzNj_xnBcE

OpenStudy (raffle_snaffle):

clear, clc % defining variables max_iter = 10; x = 2; val = 0; % values for the machlaurin series for i = 0:max_iter-1 a = x*i; b = x^2 / factorial(a); val = val + (-1)^i*b; if b < 0.001 break; end; end; % prints out results fprintf('Correct value of cos(2) is: %0.0f \n', val); disp(b);

OpenStudy (raffle_snaffle):

I think what 'break' does is that it stops running the loop once the criteria is met

OpenStudy (raffle_snaffle):

n = 0; while (n < 10) n = n + 1; a = input('Enter a value greater than 0: '); if (a <= 0) disp('You must enter a positive number ') disp('This program will terminate') break end disp('The natual log of that number is ') disp(log(a)) end

jimthompson5910 (jim_thompson5910):

I'm watching the video and it's very odd how you can program from the command line. Talk about frustrating. I'm not sure why he won't use an m file script

jimthompson5910 (jim_thompson5910):

ok I see now

OpenStudy (raffle_snaffle):

no it runs the loop over again

OpenStudy (raffle_snaffle):

The youtube vid was a poor example

jimthompson5910 (jim_thompson5910):

`no it runs the loop over again` when you enter a negative number, it doesn't break out of the 'while' loop?

OpenStudy (raffle_snaffle):

yes it breaks out of the while loop

OpenStudy (raffle_snaffle):

why would I need a break if I am not inputting any values or using if else statement

OpenStudy (raffle_snaffle):

I have another example here with mid pointbreak

jimthompson5910 (jim_thompson5910):

ok post that example

OpenStudy (raffle_snaffle):

while (1) num_candy_bars = input('Enter the number of candy bars '); if num_candy_bars < 0 disp('Must be a positive number') else total = num_candy_bars * 0.75; fprintf('The total cost is %5.2f dollars \n', total) break end end

OpenStudy (raffle_snaffle):

One issue with this strategy is that the loop need never end. In this program, if the user keeps replying with a negative number, the pogram will continue to prompt for a positive value. one way to get around thhis is to use a for loop, which has a present number of iterations. In this example we have defined 3 the maximum number of passes through the loop. for k = 1:3 num_candy_bars = input('Enter the number of candy bars '); if num_candy_bars < 0 disp('Must be a positive number') else total = num_candy_bars * 0.75; fprintf('The total cost is %5.2f dollars \n', total) break end end

OpenStudy (raffle_snaffle):

^took text from the book

jimthompson5910 (jim_thompson5910):

ok let me try something

jimthompson5910 (jim_thompson5910):

ok I think I have it

OpenStudy (raffle_snaffle):

okay

jimthompson5910 (jim_thompson5910):

here's the m file and command output that I got

OpenStudy (raffle_snaffle):

% first term of Maclaurin series y(1) = 1; % first partial sum total(1) = y(1);

OpenStudy (raffle_snaffle):

what does total(1) = y(1) does?

jimthompson5910 (jim_thompson5910):

it sets up the first partial sum

jimthompson5910 (jim_thompson5910):

basically it's equal to the first term of the series

jimthompson5910 (jim_thompson5910):

first partial sum = term1 second partial sum = term1+term2 third partial sum = term1+term2+term3 etc etc

jimthompson5910 (jim_thompson5910):

we can define this recursively nth partial sum = (n-1)th partial sum + nth term which is what I did when I wrote `total(k) = total(k-1)+y(k);`

OpenStudy (raffle_snaffle):

okay I am reading your code now and trying to understand

OpenStudy (raffle_snaffle):

Mine isn't running, looking for problem

jimthompson5910 (jim_thompson5910):

what error is it saying?

OpenStudy (raffle_snaffle):

@jim_thompson5910 fixed it.

OpenStudy (raffle_snaffle):

% defining variables max_iter = 10; tolerance = 0.001; x = 2; val = 0; % first term of Maclaurin series y(1) = 1; % first partial sum total(1) = y(1); % values for the machlaurin series for k = 2:max_iter % generate each term of the Maclaurin series y(k) = ((-1)^(k-1)) * x^((k-1)*2) /factorial((k-1)*2); % then add that last generated term (y(k)) to % the current subtotal to get the next partial sum total(k) = total(k-1) + y(k); So what is going on here. where does y(1) = 1 go? Where does total(1) = y(1) go? I might have to do this on paper to fully understand. Can you explain to me what one iteration does through the loop?

jimthompson5910 (jim_thompson5910):

y(1) = 1 means the first term of the y vector is 1 y(2) is going to be generated using the formula ((-1)^(k-1)) * x^((k-1)*2) /factorial((k-1)*2); where k is equal to 2. The value of x is ALWAYS 2 in this case. This is because we want to find the value of cos(x) = cos(2)

jimthompson5910 (jim_thompson5910):

k = 2 and x = 2 is plugged into `((-1)^(k-1)) * x^((k-1)*2) /factorial((k-1)*2)` to get -2

jimthompson5910 (jim_thompson5910):

so far, we have y(1) = 1 y(2) = -2

jimthompson5910 (jim_thompson5910):

total(1) = y(1) = 1 total(2) = y(1)+y(2) = total(1)+y(2) = 1+(-2) = -1

jimthompson5910 (jim_thompson5910):

make sense so far?

OpenStudy (raffle_snaffle):

hold on

OpenStudy (raffle_snaffle):

yes okay everything make sense so far

jimthompson5910 (jim_thompson5910):

so to find y(3), we plug in x = 2 and k = 3 into the formula `((-1)^(k-1)) * x^((k-1)*2) /factorial((k-1)*2)`

jimthompson5910 (jim_thompson5910):

x is still 2 k is the only thing that changed

OpenStudy (raffle_snaffle):

Okay I get it now.

OpenStudy (raffle_snaffle):

% check to see if the absolute difference in terms % is within the specified tolerancec if (abs(total(k) - total(k-1)) < tolerance) fprintf('So, cos(%f) = %f \n', x, total(k)); fprintf('where the angle is in radian mode \n'); fprintf('The seris stopped at k = %d\n', k); break end end basically this part will initiate if the value is less than the tolerance?

jimthompson5910 (jim_thompson5910):

yes everything between the "if..." part and the first "end" you see

OpenStudy (raffle_snaffle):

Awesome, okay thanks for your help and the explanation. It was very helpful.

jimthompson5910 (jim_thompson5910):

no problem

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!