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?
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!