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 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2016-11-26
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by