matlab help please
%% 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. %}
@phi
Okay I agree with the statement. I am just thinking
so the equation I will be using in my for loop is the one shown in the textbook... let me find it
y(k) = (((-1)^(k-1)) * ((x^((k-1)*2)) / factorial((k-1)*2));
I think we can stop when the absolute value of the term is < 0.001
% 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));
Do I need to use a break?
yes, use a break
% 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);
where do i use the break?
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.
brb
@jim_thompson5910
Mathematically I am not exactly sure what is going on here.
Its been awhile since I have taken calc 3. lol
equation in the textbook is this y(k) = (((-1)^(k-1)) * ((x^((k-1)*2)) / factorial((k-1)*2))
tbh, I've never heard of a "midpoint break loop" what is it exactly?
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);
I think what 'break' does is that it stops running the loop once the criteria is met
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
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
ok I see now
no it runs the loop over again
The youtube vid was a poor example
`no it runs the loop over again` when you enter a negative number, it doesn't break out of the 'while' loop?
yes it breaks out of the while loop
why would I need a break if I am not inputting any values or using if else statement
I have another example here with mid pointbreak
ok post that example
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
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
^took text from the book
ok let me try something
ok I think I have it
okay
here's the m file and command output that I got
% first term of Maclaurin series y(1) = 1; % first partial sum total(1) = y(1);
what does total(1) = y(1) does?
it sets up the first partial sum
basically it's equal to the first term of the series
first partial sum = term1 second partial sum = term1+term2 third partial sum = term1+term2+term3 etc etc
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);`
okay I am reading your code now and trying to understand
Mine isn't running, looking for problem
what error is it saying?
@jim_thompson5910 fixed it.
% 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?
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)
k = 2 and x = 2 is plugged into `((-1)^(k-1)) * x^((k-1)*2) /factorial((k-1)*2)` to get -2
so far, we have y(1) = 1 y(2) = -2
total(1) = y(1) = 1 total(2) = y(1)+y(2) = total(1)+y(2) = 1+(-2) = -1
make sense so far?
hold on
yes okay everything make sense so far
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)`
x is still 2 k is the only thing that changed
Okay I get it now.
% 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?
yes everything between the "if..." part and the first "end" you see
Awesome, okay thanks for your help and the explanation. It was very helpful.
no problem
Join our real-time social learning platform and learn together with your friends!