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 个评论
Geoff Hayes
2017-12-1
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.
Davide Mori
2017-12-1
编辑:Geoff Hayes
2017-12-3
Davide Mori
2017-12-2
Geoff Hayes
2017-12-3
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.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!