MATLAB Answers

# Trying to assign a 3x1 matrix to the 1st iteration of a for loop.

3 views (last 30 days)
Terry Poole on 17 Sep 2020
Commented: Terry Poole on 17 Sep 2020
clear all; close all; clc;
airspeed = 200;
windspeed = 15;
angleofattack = 15*(pi/180);
sideslip = 10*(pi/180);
roll = 12*(pi/180);
pitch = 6*(pi/180);
yaw = 0*(pi/180);
airspeed_vector= [200;0;0];
windspeed_vector = [0;15;0];
[bankangle,flightpathangle]=...
navigation_to_wind(angleofattack,sideslip,roll,pitch,yaw)
[groundspeed_vector, navigation_airspeed_vector]=...
wind_to_navigation(airspeed_vector, windspeed_vector,...
angleofattack, sideslip, roll, pitch, yaw)
%
%Stuck here, trying to use Euler method to integrate
%navigation_airspeed_vector over a time of 60 seconds with a .01 step
x0=0;%initial value for x
y0=navigation_airspeed_vector;%initial value for y (3x1 double)
xn=60;%final vaule for x(where we want to end up)
h=0.01;%This is the choice of h that changes for different parts
n=(xn-x0)/h;
x=zeros(n+1,1);
y=zeros(n+1,1);
x(1)=x0;
y(1)=y0;%%Can't figure out how to assign the 3x1 matrix to this variable,
%I'm sure it has something to do with y being a 6000x1 matrix but I can't
%figure out what to do to fix it.
for k=1:n
x(k+1)=x(k)+h;
y(k+1)=y(k)+h*(x(k)*y(k));
disp(y);
end
function [bankangle,flightpathangle]=...
navigation_to_wind(angleofattack,sideslip,roll,pitch,yaw)
Cwindtobody = [cos(angleofattack)*cos(sideslip), -cos(angleofattack)...
*sin(sideslip), -sin(angleofattack); sin(sideslip), cos(sideslip),...
0; sin(angleofattack)*cos(sideslip), -sin(angleofattack)...
*sin(sideslip), cos(angleofattack)]^(-1);
Cbodytonavigation = [cos(pitch)*cos(yaw), cos(pitch)*sin(yaw),...
-sin(pitch); sin(roll)*sin(pitch)*cos(yaw)-cos(roll)*sin(yaw),...
sin(roll)*sin(pitch)*sin(yaw)+cos(roll)*cos(yaw), sin(roll)...
*cos(pitch); cos(roll)*sin(pitch)*cos(yaw)+sin(roll)...
*sin(yaw), cos(roll)*sin(pitch)*sin(yaw)-sin(roll)...
*cos(yaw), cos(roll)*cos(pitch)];
Cnavigationtowind = Cwindtobody*Cbodytonavigation;
bankangle = (atan(Cnavigationtowind(2,3)/Cnavigationtowind(3,3)))*(180/pi);
flightpathangle = (-asin(Cnavigationtowind(1,3)))*(180/pi);
end
function [groundspeed_vector, navigation_airspeed_vector]...
= wind_to_navigation(airspeed_vector, windspeed_vector,...
angleofattack, sideslip, roll, pitch, yaw)
Cwindtobody = [cos(angleofattack)*cos(sideslip), -cos(angleofattack)...
*sin(sideslip), -sin(angleofattack); sin(sideslip), cos(sideslip),...
0; sin(angleofattack)*cos(sideslip), -sin(angleofattack)...
*sin(sideslip), cos(angleofattack)]^(-1);
Cbodytonavigation = [cos(pitch)*cos(yaw), cos(pitch)*sin(yaw),...
-sin(pitch); sin(roll)*sin(pitch)*cos(yaw)-cos(roll)*sin(yaw),...
sin(roll)*sin(pitch)*sin(yaw)+cos(roll)*cos(yaw), sin(roll)...
*cos(pitch); cos(roll)*sin(pitch)*cos(yaw)+sin(roll)...
*sin(yaw), cos(roll)*sin(pitch)*sin(yaw)-sin(roll)...
*cos(yaw), cos(roll)*cos(pitch)];
Cnavigationtowind = Cwindtobody*Cbodytonavigation;
navigation_airspeed_vector = Cnavigationtowind^(-1)*airspeed_vector;
groundspeed_vector = navigation_airspeed_vector + windspeed_vector;
end

#### 0 Comments

Sign in to comment.

### Accepted Answer

Dana on 17 Sep 2020
If y is an array that's tracking values of a 3-element vector as you iterate on k, then you need to make y a matrix. If I've understood this right, you want to do something like:
x=zeros(n+1,1);
%y=zeros(n+1,1); % the way you did it
y=zeros(3,n+1); % the correct way: now y(:,k) corresponds to the k-th 3-element vector
x(1)=x0;
%y(1)=y0; % the way you did it
y(:,1)=y0; % the correct way
for k=1:n
x(k+1)=x(k)+h;
%y(k+1)=y(k)+h*(x(k)*y(k)); % the way you did it
y(:,k+1)=y(:,k)+h*(x(k)*y(:,k)); % the correct way
disp(y);
end

#### 1 Comment

Terry Poole on 17 Sep 2020
That worked! Thanks, a bunch!

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!

Translated by