MATLAB Answers

## How do I loop through incrementally changing values

Asked by Highwayman2

### Highwayman2 (view profile)

on 5 May 2015
Latest activity Answered by David Sanchez

### David Sanchez (view profile)

on 5 May 2015
Accepted Answer by Walter Roberson

### Walter Roberson (view profile)

I have two given formulae,
a = 0.1+3*10^-3 * alpha^2
and
b= 0.2+0.1*alpha - (3*10^-3)(alpha^2)
I want to run through values of alpha that increase in small increments (0.5) from 1 to 20.
I need the results from those to then run through the formula c = tan^-1(a/b) which needs to be plotted for all the different inputs.

#### 0 Comments

Sign in to comment.

## 3 Answers ### Walter Roberson (view profile)

Answer by Walter Roberson

### Walter Roberson (view profile)

on 5 May 2015
Accepted Answer

alphavals = 1:0.5:20;
for K = 1 : length(alphavals);
alpha = alphavals(K);
a = ...
b = ...
c(K) = atan2(b,a);
end
plot(alphavals, c);
Note that I adjusted to use the four-quadrant inverse tan; the formula as you wrote it would be only the two-quadrant inverse tan. You could create that if you really wanted:
c(K) = atan(a/b);
The code here shows what you asked, looping through changing the value of alpha. But it isn't nearly as efficient as it could be. Instead, you can use
alpha = 1:0.5:20;
temp = 3E-3 .* alpha.^2; %notice the .^ instead of plain ^
a = 0.1 + temp;
b = 0.2 + 0.1 .* alpha - temp;
c = atan2(b, a);
plot(alpha, c);

#### 0 Comments

Sign in to comment.

Answer by Nobel Mondal

### Nobel Mondal (view profile)

on 5 May 2015

You could utilize the colon and element-wise operators, instead of looping.
For example:
alpha = 1:0.5:20;
a = 0.1 + 3* 10^-3 * alpha.^2

#### 0 Comments

Sign in to comment.

Answer by David Sanchez

### David Sanchez (view profile)

on 5 May 2015

alpha = 1:0.5:20;
a = 0.1+3*10^-3 * alpha.^2;
b = 0.2+0.1*alpha - (3*10^-3)*(alpha.^2);
c = 1./tan(-a./b); % I don't know what is your equation
plot3(a,b,c) #### 0 Comments

Sign in to comment.