matlab
You forgot to add -1^n for your Taylor series of sin(x).
@yagosik thanks
@raffle_snaffle No problem.
Sorry, but what do you need to get as a result?
@ikram002p
My code isn't running in the order I want it to run. The assignment is attached.
I just can't get the prompts correct. I want it to ask the user... Which func would you like to use? say i choose 1 The func chosen is: 1 next... Input an x value: says we enter 2 then it ends. I need the code to run properly if I chose either func 1 or 2.
@phi
First, I don't understand your use of the variable "func"
clear, clc % clears command and workspace window % define variables max_iters = 1:150; n = 0; allowed_error = 0.005; % function_1 is eulers % function_2 is sin % asking user which function to operate func = input('Which function would you like to use, 1 or 2: '); if (func == 1) fprintf('You chose function 1. '); end % asking user to input a x value x = input('Input an x value: '); if ((x >= -50) && (x <= 50)) elseif error ('x must be x >= -50 and x <=50 ') end % while loop for eulers while (n <= max_iters) func = (x^(n))/(factorial(n)); n = n + 1; if (func <= allowed_error) break end end if (func == 2) fprintf('You chose function 2. '); end % asking user to input a x value x = input('Input an x value: '); if ((x >= -50) && (x <= 50)) elseif error ('x must be x >= -50 and x <= 50') end % while loop for sin while (n <= max_iters) func = ((-1)^n)*(x^(2*n+1))/(factorial(2*n+1)); n = n + 1; fprintf('continue iterations'); if (func <= allowed_error) break end end
I am sorry @phi I misunderstood your question
func is either eulers or sin
The assignment is very specific about what they want, and your code is not very close to what they want.
It is easier to debug if it's not a function (though you could use the debugger) but they are expecting you to write a function with 4 parameters, and it returns a 0 or 1, and the number of iterations it takes to converge (if it did)
the input parameter func is supposed to be a string, e.g. 'sin' or 'exp' they want you to use a "switch" statement, not if/then
okay
meanwhile, *** if ((x >= -50) && (x <= 50)) elseif error ('x must be x >= -50 and x <= 50') end *** the syntax/logic should be if ( (x< -50) || (x>50) ) error ('x must be x >= -50 and x <= 50'); end;
your statement: *** if ((x >= -50) && (x <= 50)) elseif error ('x must be x >= -50 and x <= 50') end *** the elseif error part is not strictly correct. slightly better would be if ((x >= -50) && (x <= 50)) else error ('x must be x >= -50 and x <= 50') end but that is a bit muddled. the most straight forward way to do the test is to "throw an error" if x is out of bounds: if ( (x< -50) || (x>50) ) error ('x must be x >= -50 and x <= 50'); end;
"or" instead of an "and", okay.
we can fix up your code, but it is not what the assignment asks for.
Do I need to use switch, instead of if else statements? I am not familiar with "switch"
So both functions, eulers and sin are going to be in the same m file or have their own m file?
rather than ask the user for input, I would "hard-wire" the input... because after you get the script to execute properly, you will change it into a function.
if you are going to create a function, but use a script for debugging, I would start with %function [converges, iter] = ... % taylor series(func, x, allowed_error , max_iters) and then copy the comments given in the problem. next, I would define values for the params (these are test values) % REMOVE in the real function func= 'sin'; x= 0.5; allowed_error= 1e-5; max_iters= 100; % REMOVE in the real function
Let me think
the first thing we do is check the input variables. Something like this: % check that input values are within bounds if ( (x<-50) || (x>50) ) error('x must be between -50 and +50'); end; if (allowed_error< 1e-12) error('allowed error must be >= 1e-12'); end; if ( (max_iters<1) || (max_iters>150)) error('max number of iterations must be from 1 to 150'); end; % check that func is a valid string validStrings = {'sin','exp'}; validStr = validatestring(func,validStrings)
next, for each func choice, evaluate the taylor series terms. We don't care about the sign (we want positive terms), so we can ignore (-1) part in the sine % taylor series terms converges= 0; iter= 0; switch func case 'sin' % taylor series for n=0:(max_iters-1) kk= 2*n+1; tt= x^kk/factorial(kk); if (tt<allowed_error) converges= 1; iter= n+1; break; end; %if we converged end; % for case 'exp' for n=0:(max_iters-1) tt= x^n/factorial(n); if (tt<allowed_error) converges= 1; iter= n+1; break; end; %if we converged end; % for otherwise error('invalid function name'); end; % switch % REMOVE in the real function fprintf('%s converges %d iter %d\n', func, converges, iter); % REMOVE in the real function
% define variables n = 0; % function 1 is eulers % function 2 is sin iter = 0; converges = 0; % if else statement for eulers if (func == 'exp') % while loop for eulers func = (x^(n))/(factorial(n)); n = n + 1; if (func <= allowed_error) converges = 1; iter = n; break end end else fprintf('You chose function 2. \n'); % while loop for sin while (n <= max_iters) func = ((-1)^n)*(x^(2*n+1))/(factorial*(2*n+1)); n = n + 1; if (abs(func) <= allowed_error) convergs = 1; iter = n; break end end end
I think the assignment calls for while loops not for loops.
Join our real-time social learning platform and learn together with your friends!