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!