4

I am writing a code which will need values of $\sin(x)$ for $x = 2^2, 2^3, 2^4, \dots, 2^{25}$

Being fairly new at matlab still, I am unsure how to do this in the most efficient manner.

I know

n=0:10
0 1 2 3 4 5 6 7 8 9 10

But putting in something like

n = 2^2:2^x:2^25

Doesn't make sense, so what would be another way I could do this?

I suppose I could let

x=3:24

But where would I proceed for there?

elbarto
  • 3,356

1 Answers1

1

The following script should work fine:

% Initiate the loop, i.e., the power of 2, from n=2 to n=25
for n = 2:25

    % Find the value of 2 raised to the power of n where n ranges from 2 to 25
    x = 2^n;

    % Evaluate the value of sin at x and store it in an array y (row vector)
    y(n) = sin(x); 
end

Let me know if you have any problems.

user56031
  • 496
  • 1
    thank you a lot!

    I will give this a try and report back. Although it's not exactly sin(x) which I am computing so it may take a while for me to get back to you, as there is some other work ! :P

    – elbarto Mar 18 '15 at 09:52
  • Hey @user56031 I just wanted to ask a follow up question.

    I want to display the difference between y(n) and pi, but for EACH instance of n.

    As of the moment, I have

    disp(abs(y(n)-pi)), however this only gives me the error for the value at n=25, instead of n=1,2,3,...,25

    – elbarto Mar 19 '15 at 18:39
  • Whoops! sorry, I think I got it. I was writing that outside the for-loop, but then I moved the disp codes I wanted inside and it worked. nevermind! – elbarto Mar 19 '15 at 18:48