MATLAB: How to integrate different values into 4 tables

for looptable

I have this code
clc
clear
close
a = 40*(pi/180);
v = 1200;
g = 32.2;
k=[0,2e-6,10e-6,20e-6];
k=0;
dt=.5;
t = 0:dt:55;
v = zeros(length(t),4);
vx = zeros(length(t),4);
vy= zeros(length(t),4);
x = zeros(length(t),4);
y = zeros(length(t),4);
v(1,1) = 1200;
vx(1,1) = v(1,1)*cos(a);
vy(1,1) = v(1,1)*sin(a);
x(1,1)=0;
y(1,1)=0;
for i=2:length (t)
x(i,1) = x(i-1,1)+vx(i-1,1)*dt-.5*k*v(i-1,1).^2*cos(a)*dt.^2;
y(i,1) = y(i-1,1)+vy(i-1,1)*dt-.5*k*v(i-1,1).^2*sin(a)*dt^2-0.5*g*dt.^2;
vx(i,1) = vx(i-1,1)-k*v(i-1,1).^2*cos(a)*dt;
vy(i,1) = vy(i-1,1)-k*v(i-1,1).^2*sin(a)*dt-g*dt;
a=atan(vy(i,1)./vx(i,1));
v(i,1)=sqrt(vx(i,1).^2+vy(i,1).^2);
end
vx(1:round(5/dt):end,:)
vy(1:round(5/dt):end,:)
x(1:round(5/dt):end,:)
y(1:round(5/dt):end,:)
That results in
ans =
919.25 0 0 0
919.25 0 0 0
919.25 0 0 0
919.25 0 0 0
919.25 0 0 0
919.25 0 0 0
919.25 0 0 0
919.25 0 0 0
919.25 0 0 0
919.25 0 0 0
919.25 0 0 0
919.25 0 0 0
ans =
771.35 0 0 0
610.35 0 0 0
449.35 0 0 0
288.35 0 0 0
127.35 0 0 0
-33.65 0 0 0
-194.65 0 0 0
-355.65 0 0 0
-516.65 0 0 0
-677.65 0 0 0
-838.65 0 0 0
-999.65 0 0 0
ans =
0 0 0 0
4596.27 0 0 0
9192.53 0 0 0
13788.80 0 0 0
18385.07 0 0 0
22981.33 0 0 0
27577.60 0 0 0
32173.87 0 0 0
36770.13 0 0 0
41366.40 0 0 0
45962.67 0 0 0
50558.93 0 0 0
ans =
0 0 0 0
3454.23 0 0 0
6103.45 0 0 0
7947.68 0 0 0
8986.90 0 0 0
9221.13 0 0 0
8650.35 0 0 0
7274.58 0 0 0
5093.81 0 0 0
2108.03 0 0 0
-1682.74 0 0 0
-6278.52 0 0 0
My issues is whenever I change the K value to [2e-6,10e-6, or 20e-6] it results in the first column changing to the respective k values. However, if I change the x(i,1) positions to say x(i,2) for [2e-6] (and all subsequent notations) then I get every k = 2e-6 value in the 2nd column and the first column becomes zero. Question is how do I get the answers to add on to the table made from previous calculations?

Best Answer

I modified the code for you and i posted the ans also, i hope this is what u wanted.....
clc
clear
close
format long
k1=[0,2e-6,10e-6,20e-6];
dt=.5;
t = 0:dt:55;
g = 32.2;
v = zeros(length(t),4);
vx = zeros(length(t),4);
vy= zeros(length(t),4);
x = zeros(length(t),4);
y = zeros(length(t),4);
for j=1:length(k1)
a = 40*(pi/180);
k=k1(j);
v(1,j) = 1200;
vx(1,j) = v(1,j)*cos(a);
vy(1,j) = v(1,j)*sin(a);
x(1,j)=0;
y(1,j)=0;
for i=2:length (t)
x(i,j) = x(i-1,j)+vx(i-1,j)*dt-.5*k*v(i-1,j).^2*cos(a)*dt.^2;
y(i,j) = y(i-1,j)+vy(i-1,j)*dt-.5*k*v(i-1,j).^2*sin(a)*dt^2-0.5*g*dt.^2;
vx(i,j) = vx(i-1,j)-k*v(i-1,j).^2*cos(a)*dt;
vy(i,j) = vy(i-1,j)-k*v(i-1,j).^2*sin(a)*dt-g*dt;
a=atan(vy(i,j)./vx(i,j));
v(i,j)=sqrt(vx(i,j).^2+vy(i,j).^2);
end
end
vx(1:round(5/dt):end,:)
vy(1:round(5/dt):end,:)
x(1:round(5/dt):end,:)
y(1:round(5/dt):end,:)
ans =
1.0e+02 *
9.192533317427737 9.192533317427737 9.192533317427737 9.192533317427737
9.192533317427737 9.087437198565105 8.688194539448844 8.232369634405721
9.192533317427737 8.992683198820997 8.271606474129895 7.515600132134569
9.192533317427737 8.906142118918728 7.919402869533692 6.956903130394356
9.192533317427737 8.825438093191325 7.611669801834986 6.497663684523137
9.192533317427737 8.747994917296394 7.330900766229695 6.094617348287314
9.192533317427737 8.671191392627970 7.062225373628980 5.716607364427984
9.192533317427737 8.592588049635696 6.794295817589458 5.344401073260919
9.192533317427737 8.510130827164266 6.519858929429604 4.969536950219919
9.192533317427737 8.422255391440707 6.235515863016696 4.591412572439150
9.192533317427737 8.327886941553478 5.940899030969548 4.213955536697458
9.192533317427737 8.226377828143628 5.637702715980655 3.842942121374995
ans =
1.0e+02 *
7.713451316238471 7.713451316238471 7.713451316238471 7.713451316238471
6.103451316238469 6.023419388685378 5.719372277363858 5.372185731794696
4.493451316238467 4.358060751416640 3.869349418126093 3.356497018654969
2.883451316238464 2.713013480784294 2.125068241342515 1.550149352309664
1.273451316238464 1.084948020280437 0.460462096125176 -0.114597066820229
-0.336548683761536 -0.528223533354256 -1.139760914385037 -1.672277710014929
-1.946548683761535 -2.127194956892446 -2.681175666264873 -3.132854106675353
-3.556548683761536 -3.711278137353608 -4.161511360325129 -4.490534191556887
-5.166548683761538 -5.278613302973931 -5.573538912210908 -5.733287819377662
-6.776548683761540 -6.826502519553036 -6.908138272916148 -6.850276415575496
-8.386548683761543 -8.351753997529265 -8.156695586961243 -7.835785114702563
-9.996548683761544 -9.850966102443524 -9.312567227724861 -8.690247541356209
ans =
1.0e+04 *
0 0 0 0
0.459626665871387 0.456952509660115 0.446601570599481 0.434388168668790
0.919253331742773 0.908917067719421 0.870286135308686 0.827286469285742
1.378879997614160 1.356358536870645 1.274838854896066 1.188582146706933
1.838506663485547 1.799629168051811 1.662970072505337 1.524634799788792
2.298133329356934 2.238956995531161 2.036456085149122 1.839283305588884
2.757759995228320 2.674439272489372 2.396260757987131 2.134509243829672
3.217386661099707 3.106045768985768 2.742688806528300 2.411035206651566
3.677013326971094 3.533633248951142 3.075579187877789 2.668900501599922
4.136639992842481 3.956967824055119 3.394507100925129 2.907931858633309
4.596266658713867 4.375749794739506 3.698957566875462 3.128051985769669
5.055893324585254 4.789636722968088 3.988452648518830 3.329435350798285
ans =
1.0e+04 *
0 0 0 0
0.345422565811923 0.343309821893089 0.335128870726984 0.325469355578336
0.610345131623847 0.602754750055932 0.574341837338965 0.542628698537214
0.794767697435771 0.779455549117251 0.723828388470866 0.664600119087606
0.898690263247694 0.874339858815437 0.788177334077686 0.699997622986476
0.922112829059617 0.888199047586014 0.770947619004973 0.654920194605361
0.865035394871541 0.821754585219541 0.675181896918930 0.534387622251712
0.727457960683465 0.675728278727461 0.503850396003435 0.343355037091190
0.509380526495388 0.450906840766734 0.260173936315252 0.087260126553497
0.210803092307311 0.148192401693298 -0.052206230259897 -0.227864175692256
-0.168274341880766 -0.231364338893490 -0.429197553309034 -0.595561742932445
-0.627851776068843 -0.686546835784460 -0.866321819027821 -1.009244632798958
Related Question