If I have functions written that require a user input, how would I test different points without copy and pasting the code over and over again?

1 view (last 30 days)
This is the code I have:
x = input('Type a value for x: ');
for i = 1:9
vec(i) = (-1)^(i+1)*x^(2*i-1)/factorial(2*i-1);
end
exct = sin(x)
app = sum(vec)
err = abs(app - exct)
% ii
app1 = 0;
for i = 1:100
app1 = app1 + (-1)^(i+1)*x^(2*i-1)/factorial(2*i-1);
end
exct = sin(x)
app1
err1 = abs(app1 - exct)
app2 = 0;
for i = 1:250
app2 = app2 + (-1)^(i+1)*x^(2*i-1)/factorial(2*i-1);
end
exct = sin(x)
app2
err2 = abs(app2 - exct)
-------------------------------
I would like to test this code for x = 2, 2.5, 3. My problem is that since x is defined as user input which is entered in the command window, how would I evaluate the code within the script at these points without retyping the code 3 times with the respective values of x that I am trying to test?

Accepted Answer

Walter Roberson
Walter Roberson on 26 Nov 2016
You can wrap this in a for loop or while loop. Or you could store it in a file and then run the file repeatedly.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!