Vector with 4 angles?

Hi! How can I create a vector of 4 angles( named "phi") with the property that each angle has a range between 0-2*pi? I need to access in every single element in a FOR cycle like that:
f1=@(t,phi) w - sigma*N*cos(phi(1));
f2=@(t,phi) w - sigma*N*cos(phi(2));
f3=@(t,phi) w + sigma*N*cos(phi(3));
f4=@(t,phi) w - sigma*N*cos(phi(4));
for i=(1:4)
[t1,phi(1)]=ode45(f1,t1,0);
[t2,phi(2)]=ode45(f2,t2,0);
[t3,phi(3)]=ode45(f3,t3,0);
[t4,phi(4)]=ode45(f4,t4,0);
if (0<=phi(i)<pi) || (pi<=phi(i)<2*pi) || (2*pi<=phi(i)<pi)
N = N*(-1);
end
end
but I need to give that property to every single angle of the vector. Thanks!

4 Comments

Davide - I don't understand how the above code works. For example, why iterate four times and re-calculate
[t1,phi(1)]=ode45(f1,t1,0);
when this doesn't depend on i. Also, wouldn't this throw an error because the third parameter to ode45 (in every case) is a scalar 0 but then
f2=@(t,phi) w - sigma*N*cos(phi(2));
seems to expect an array phi of up to four elements.
Wait wait, you're right, my fault. I will explain myself better. So, I have 4 ODEs of which at the end of the computation I want to plot the sin (phi (i)). The result of each equation, however, depends on the sign assumed by N, which I would like to be a number that can vary between -1 and 1. As soon as the phases switch to pi or 2*pi, N switches from -1 to + 1 respectively. My problem is how to define the vector phi with the property of varying between 0 and 2 * pi and how to create N in such a way that it varies between -1 and 1. The code is this:
phi= zeros(1,4);
N=(-1:0.01:1);
w= 0.04;
sigma= 0.0052;
t1=(0:1:14);
t2=(0:1:14);
t3=(0:1:14);
t4=(0:1:14);
f1=@(t,phi) w - sigma*N*cos(phi(1));
f2=@(t,phi) w - sigma*N*cos(phi(2));
f3=@(t,phi) w + sigma*N*cos(phi(3));
f4=@(t,phi) w - sigma*N*cos(phi(4));
[t1,phi(1)]=ode45(f1,t1,0);
[t2,phi(2)]=ode45(f2,t2,0);
[t3,phi(3)]=ode45(f3,t3,0);
[t4,phi(4)]=ode45(f4,t4,0);
for i=(1:4)
if (0<=phi(i)<pi) || (pi<=phi(i)<2*pi) || (2*pi<=phi(i)<pi)
N = N*(-1);
end
end
plot(t1,sin(phi(1)),'r-',t2,sin(phi(2)),'v-',t3,sin(phi(3)),'k-',t4,sin(phi(4)),'b-')
xlabel('time')
ylabel('sin(phi)')
as you can see, at the beginning I had tried to create a vector of zeros to be filled with the different phases thanks to the resolution of the equations, but I do not think it is the right way to proceed.
So I modified my code in this way(attached file) but I get these errors:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in tesi1 (line 20) [t,phi(1)]=ode45(f1,t,0);
can Anyone help me?
Davide - look closely at the result being returned by ode45
[a, b] = ode45(f1,t,0)
Both a and b are 15x1 arrays and you are trying to store the second result (b) into a single element
[t,phi(1)]=ode45(f1,t,0);
and so the error makes sense. I suspect that you could do something like
phi = cell(4,1);
[t,phi{1}]=ode45(f1,t,0);
etc. But then you will still face the same error as before
f2=@(t,phi) omega - sigma*N(2)*cos(phi(2));
Once again, your inputs to f2 are scalars, but you treat phi as an array and so you will get another error
Attempted to access phi(2); index out of bounds because numel(phi)=1.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 1 Dec 2017

Commented:

on 3 Dec 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!