-1

For my problem I need to iterate numbers 1 to n through the equation 1/(1+25x^2) my current code is

for i = 1:n
result = 1/(1+25*i^2);
end

But this is only outputting one number I want to save the numbers into an array so I can plot them

1 Answers1

1

You can write it as

result = 1./(1+25*(1:n).^2);

or in a for loop with indexing

result = zeros(1,n);
for j = 1:n
  result(j) = 1/(1+25*j^2);
end
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • You might want to add that preallocation is important, especially given the OP's proven lack of basic knowledge. Also I advice against using `i` or `j` as variables, and especially recommending to use them to new MATLAB users, because [they denote the imaginary unit](https://stackoverflow.com/q/14790740/5211833) and can lead to hard to find and debug errors. – Adriaan Sep 16 '20 at 11:51