index out of bounds because numel(v)=1.

2 views (last 30 days)
I'm working with a for loop to recalculate the values of x and v but I'm getting an error for v(2) can some one explain what I'm doing wrong!
this is the code-data
T=0.02;
N=length(ag);
t=0:T:(N-1)*T;
a1 =-475.38 ;
a2 = -0.5404;
x0=0;
v=0;
for k=1:N;
x(k)=x0+T*v
v(k)=a1*x(k)+a2*v(k)-ag(k)
end
below is the data and the code I'm using.
[-2.43235440000000;-1.40536032000000;-0.382227120000000;-1.65245664000000;-2.92654704000000;-4.19677656000000;-2.63312016000000;-1.06946376000000;0.494192640000000;-1.42080384000000;-3.33580032000000;-5.25079680000000;-2.80685976000000;-0.362922720000000;-1.62156960000000;-0.853254480000000;-0.0810784800000000;-1.71423072000000;-3.34738296000000;-4.98053520000000;-6.61368744000000;1.32428184000000;9.26611200000000;3.82999296000000;-1.60612608000000;-2.03854464000000;-6.38203464000000;-10.7293855200000;-15.0728755200000;-9.45529512000000;-3.84157560000000;-3.71030568000000;-3.57517488000000;-3.44390496000000;1.87638768000000;7.19668032000000;12.5169729600000;12.9918612000000;22.0958162400000;17.5052299200000;12.9185044800000;12.3586768800000;11.7988492800000;11.2390216800000;10.6791940800000;15.8913820800000;21.1035700800000;26.3157580800000;31.5279460800000;26.4315844800000;21.3390837600000;16.2465830400000;16.4434879200000;16.6442536800000;9.37421664000000;2.10417960000000;-5.16585744000000;-12.4358944800000;-19.7059315200000;-26.9759685600000;-34.2460056000000;-17.4666211200000;-0.691097520000000;16.0882869600000;32.8676714400000;49.6431950400000;66.4225795200000;49.8362390400000;33.2537594400000;34.3695537600000;35.4892089600000;36.6088641600000;35.9988451200000;35.3888260800000;36.5934206400000;37.7941543200000;49.8130737600000;29.5434537600000;9.26997288000000;-10.9996471200000;-31.2692671200000;-51.5427480000000;-71.8123680000000;-92.0819880000000;-84.9123338400000;-77.7388188000000;-70.5653037600000;-63.3956496000000;-56.2221345600000;-62.3261858400000;-68.4340980000000;-51.0215292000000;-33.6089604000000;-16.2002524800000;1.21231632000000;18.6248851200000;36.0374539200000;53.4461618400000;70.8587306400000;88.2712994400000;105.680007360000;123.092576160000;96.6146611200000;70.1367460800000;43.6626919200000;17.1847768800000;-9.29313816000000;-35.7710532000000;-62.2489682400000;-88.7268832800000;-115.204798320000;-89.5608333600000;-63.9130075200000;-38.2690425600000;-12.6250776000000;13.0188873600000;38.6628523200000;64.3068172800000;89.9546431200000;115.598608080000;1.62543048000000;-112.347747120000;-86.4064944000000;-60.4691025600000;-34.5278498400000;-8.58659712000000;17.3546556000000;-7.08085392000000;-31.5163634400000;-55.9480120800000;-80.3835216000000;-73.2524762400000;-66.1175700000000;-53.1218479200000;-40.1261258400000;-27.1304037600000;-14.1346816800000;-1.13895960000000;11.8567624800000;2.16595368000000;-7.52099424000000;-17.2118030400000;-24.9721718400000;-32.7325406400000;-40.4890485600000;-22.7598876000000;-5.03072664000000;12.6984343200000;30.4314561600000;13.7292892800000;-2.97673848000000;-19.6789053600000;-3.91107144000000;11.8567624800000;27.6284572800000;43.3962912000000;59.1641251200000;43.6819963200000;28.1998675200000;12.7177387200000;-2.76052920000000;24.5165880000000;51.7937052000000;79.0708224000000;48.1915041600000;17.3160468000000;-13.5632714400000;-44.4387288000000;-75.3180470400000;-47.4926848800000;-19.6673227200000;8.15803944000000;35.9834016000000;10.2815234400000;-15.4242156000000;-41.1299546400000;-66.8356936800000;-43.5623090400000;-20.2889244000000;2.98059936000000;-4.10797632000000;-11.1965520000000;-18.2889885600000;-25.3775642400000;-7.80283848000000;9.76802640000000;27.3388912800000;15.8566341600000;4.37437704000000;-1.11193344000000;-6.59824392000000;-12.0884152800000;8.79508464000000;29.6747236800000;50.5582236000000;71.4417235200000;55.3920453600000;39.3423672000000;23.2965499200000;7.24687176000000;-8.80280640000000;3.84543648000000;16.4936793600000;8.28930936000000;0.0810784800000000;-8.12329152000000;5.63302392000000;19.3893393600000;33.1456548000000;46.9019702400000;60.6582856800000;74.4146011200000;88.1709165600000;70.0556676000000;51.9404186400000;33.8251696800000;15.7099207200000;-2.40532824000000;-20.5244380800000;-38.6396870400000;-56.7549360000000;-37.6590235200000;-18.5631110400000;0.532801440000000;-19.8487840800000;-40.2303696000000;-60.6119551200000;-80.9974015200000;-101.378987040000;-65.6195164800000;-29.8600459200000;5.89556376000000;41.6550343200000;77.4145048800000;26.1999316800000;-25.0146415200000;-6.45153048000000;12.1115805600000;30.6746916000000;49.2378026400000;67.8009136800000;86.3640247200000;104.927135760000;61.1988088800000;17.4704820000000;-26.2617057600000;-69.9900326400000;-55.8437683200000;-41.6975040000000;-27.5551005600000;-13.4088362400000;-37.3192660800000;-61.2335568000000;-85.1439866400000;-70.6386604800000;-56.1294734400000;-41.6202864000000;-27.1149602400000;-12.6057732000000;-25.6709911200000;-38.7362090400000;-51.8014269600000;-39.9099165600000;-28.0184061600000;-16.1268957600000;-4.23538536000000;7.65612504000000;-17.1345854400000;-41.9291568000000;-66.7198672800000;-40.2149260800000;-13.7099848800000;12.7988172000000;39.3037584000000;28.0377105600000;16.7716627200000;5.50561488000000;-5.76043296000000;7.81828200000000;21.4008578400000;34.9795728000000;48.5621486400000;62.1408636000000;75.7234394400000;57.0792499200000;38.4350604000000;19.7947317600000;1.15054224000000;7.53643776000000;13.9184724000000;20.3043679200000;16.1462001600000;11.9918932800000;11.2081346400000;10.4205151200000;-9.71011320000000;-6.83375760000000;-8.54412744000000;-10.2544972800000;-1.61770872000000;7.02294072000000;15.6635901600000;24.3003787200000;9.33174696000000;-5.63688480000000;-20.6055165600000;-9.37421664000000;1.85322240000000;13.0845223200000;2.15051016000000;-8.77964112000000;-2.62153752000000;3.53270520000000;9.68694792000000;15.8411906400000;21.9992942400000;7.04996688000000;-7.89936048000000;-1.75283952000000;4.39368144000000;0.830089200000000;-2.73350304000000;-1.91499648000000;-1.10035080000000;-0.285705120000000;2.06170992000000;4.40526408000000;-1.39377768000000;-7.19281944000000;-12.9918612000000;-18.7909029600000;-11.7370752000000;-4.68324744000000;2.37058032000000;9.42440808000000;-5.30871000000000;-4.24310712000000;-3.17750424000000;-2.11190136000000;-3.13503456000000;-4.15816776000000;2.67172896000000;9.50162568000000;16.3315224000000;23.1614191200000;29.9913158400000;36.8250734400000;23.9722039200000;11.1193344000000;-1.72967424000000;-14.5825437600000;-6.84534024000000;0.891863280000000;8.62906680000000;-6.91483608000000;-22.4548780800000;-14.4319694400000;-6.40906080000000;1.61384784000000;9.63675648000000;17.6596651200000;7.99588248000000;-1.66790016000000;-11.3316828000000;-5.89170288000000;-6.97274928000000;-8.05379568000000;-3.06167784000000;1.93430088000000;6.93027960000000;11.9262583200000;7.10788008000000;2.28950184000000;-2.52887640000000;9.72555672000000;21.9799898400000;15.6172596000000;9.25839024000000;2.89566000000000;-3.46320936000000;-1.48257792000000;0.498053520000000;2.47868496000000;4.46317728000000;10.1116447200000;15.7601121600000;21.4085796000000;16.8566020800000;12.3084854400000;26.8871683200000;21.7521979200000;16.6133666400000;11.4745353600000;6.33956496000000;1.20073368000000;-3.93809760000000;-9.07306800000000;-14.2118992800000;-19.3468696800000;-9.40510368000000;0.536662320000000;10.4784283200000;1.19301192000000;-8.09240448000000;-17.3778208800000;-26.6632372800000;-22.2888602400000;-17.9144832000000;-13.5401061600000;-12.9609741600000;-12.3818421600000;-11.8027101600000;-12.5478600000000;-13.2968707200000;-14.0420205600000;-5.20446624000000;3.63694896000000;12.4745032800000;11.5710573600000;11.9494236000000;12.3239289600000;9.99195744000000;7.65998592000000;5.32415352000000;2.99218200000000;5.59441512000000;8.19664824000000;-5.88012024000000;-19.9607496000000;-34.0375180800000;-48.1181474400000;-62.1949159200000;-50.1412485600000;-38.0837203200000;-26.0261920800000;-13.9686638400000;-1.91113560000000;-1.62156960000000;-1.33200360000000;-1.03857672000000;22.8641313600000;46.7629785600000;70.6656866400000;46.4965778400000;22.3236081600000;-1.84936152000000;-26.0223312000000;-50.1953008800000;-32.3271482400000;-14.4589956000000;-26.9450815200000;-39.4311674400000;13.5787149600000;66.5885973600000;53.1372914400000;39.6821246400000;26.2308187200000;12.7795128000000;14.0806293600000;15.3817459200000;1.99607496000000;-11.3895960000000;-24.7752669600000;-38.1570770400000;-51.5427480000000;-22.8718531200000;5.80290264000000;34.4737975200000;63.1446924000000;23.5359244800000;-16.0767043200000;-5.98822488000000;4.09639368000000;14.1848731200000;24.2733525600000;34.3579711200000;20.9645784000000;7.57118568000000;-5.82220704000000;-19.2155997600000;-32.6089924800000;-19.3932002400000;-6.17740800000000;7.03838424000000;20.2541764800000;33.4699687200000;26.1342967200000;18.8024856000000;11.4668136000000;4.13500248000000;-3.20066952000000;1.21231632000000;-11.4513700800000;-24.1150564800000;0.903445920000000;25.9219483200000;15.6404248800000;5.35890144000000;-4.91876112000000;-3.10800840000000;-11.6753011200000;-20.2425938400000;-9.07692888000000;2.08873608000000;13.2505401600000;24.4162051200000;35.5780092000000;46.7436741600000;32.6244360000000;18.5090587200000;4.38982056000000;-9.72941760000000;-23.8486557600000;-15.5516246400000;-7.25845440000000;-17.2040812800000;-27.1497081600000;-37.0953350400000;-47.0409619200000;-24.5165880000000;-1.99607496000000;20.5282989600000;12.0613891200000;3.59061840000000;-4.87629144000000;-13.3470621600000;-12.6752690400000;-12.0034759200000;-11.3316828000000;-17.4164296800000;-23.5011765600000;-29.5859234400000;-35.6706703200000;-22.1691729600000;-8.66767560000000;4.83382176000000;-2.62539840000000;-10.0807576800000;-17.5399778400000;-6.06544248000000;5.41295376000000;16.8874891200000;28.3658853600000;15.4049112000000;2.44393704000000;-10.5170371200000;-23.4741504000000;-14.1655687200000;-4.85698704000000;4.45159464000000;13.7601763200000;2.61381576000000;-8.53254480000000;-19.6827662400000;-30.8291268000000;-26.6979852000000;-22.5668436000000;-18.4357020000000;-14.3084212800000;-10.1772796800000;-22.4780433600000;-34.7826679200000;-47.0872924800000;-38.8752007200000;-30.6669698400000;-22.4548780800000;-14.2427863200000;-6.03455544000000;2.17753632000000;10.3857672000000;18.5978589600000;26.8099507200000;35.0181816000000;43.2302733600000;44.4811984800000;45.7359844800000;46.9869096000000;48.2378347200000;63.7045200000000;79.1673444000000;60.6660074400000;42.1646704800000;23.6633335200000;5.16199656000000;-13.3393404000000;-31.8406773600000;-29.2500268800000;-26.6632372800000;-24.0764476800000;-33.7247868000000;-43.3769868000000;-53.0253259200000;-47.0062140000000;-40.9871020800000;-34.9679901600000;-28.9488782400000;-30.9295096800000;-32.9101411200000;-34.8907725600000;-23.9683430400000;-13.0420526400000;-2.11962312000000;8.80666728000000;21.0186307200000;15.5593464000000;10.0962012000000;4.63691688000000;7.82986464000000;11.0228124000000;24.1034738400000;13.6057411200000;3.10800840000000;19.1036342400000;14.0651858400000;9.02287656000000;13.0034438400000;7.25459352000000;1.50188232000000;-4.24696800000000;-9.99581832000000;-5.58283248000000;-1.16984664000000;3.24313920000000;-1.78758744000000;-6.81831408000000;-11.8490407200000;-16.8797673600000;-8.35880520000000;0.162156960000000;8.68311912000000;17.2040812800000;14.0458814400000;10.8838207200000;7.72562088000000;4.56356016000000;9.43985160000000;14.3122821600000;19.1847127200000;22.7096961600000;26.2346796000000;29.7558021600000;33.2807856000000;36.8057690400000;24.2308828800000;11.6521358400000;-0.922750320000000;-13.4976364800000;-16.9840111200000;-20.4665248800000;-12.2621548800000;-4.05778488000000;4.14272424000000;12.3470942400000;20.5514642400000;-0.718123680000000;-21.9915724800000;-7.66384680000000;6.66387888000000;20.9954654400000;4.64849952000000;-11.7023272800000;-28.0492932000000;-44.3962591200000;-27.9411885600000;-11.4861180000000;4.97281344000000;-4.67938656000000;-14.3277256800000;-13.5787149600000;-12.8297042400000;-7.15421064000000;-1.47871704000000;-1.32042096000000;8.42057928000000;18.1615795200000;27.9025797600000;37.6435800000000;47.3845802400000;32.1109389600000;16.8411585600000;1.57137816000000;-13.7022631200000;-28.9720435200000;-44.2456848000000;-29.9951767200000;-15.7446686400000;-1.49416056000000;-1.09648992000000;-0.702680160000000;21.2850314400000;-18.2696841600000;-20.1653762400000;-22.0649292000000;-23.9606212800000;-25.8601742400000;-27.7558663200000;-10.4436804000000;6.86850552000000;24.1806914400000;41.4928773600000;58.8089241600000;48.6123400800000;38.4157560000000;28.2191719200000;18.0225878400000;7.82600376000000;-2.37058032000000;-12.5671644000000;-3.31649592000000;5.93417256000000;15.1809801600000;24.4316486400000;12.8258433600000;1.21617720000000;-10.3896280800000;-4.61761248000000;1.15826400000000;-1.29339480000000;-3.74505360000000;-6.19671240000000;-8.64451032000000;-16.2736092000000;-23.9027080800000;-31.5318069600000;-13.4242797600000;4.67938656000000;5.05389192000000;5.43225816000000;20.3622811200000;9.82207872000000;-0.718123680000000;-11.2583260800000;-21.7985284800000;-32.3387308800000;-6.77198352000000;18.7986247200000;8.00746512000000;-2.78755536000000;-13.5787149600000;2.03854464000000;17.6519433600000;33.2692029600000;26.8717248000000;20.4742466400000;14.0767684800000;7.67929032000000;1.28181216000000;-5.11566600000000;-11.5131441600000;-4.25082888000000;3.01534728000000;10.2776625600000;2.17367544000000;-5.93031168000000;-14.0342988000000;-22.1382859200000;-12.1965199200000;-2.25475392000000;7.69087296000000;0.776036880000000;-6.13493832000000;3.95354112000000;14.0381596800000;24.1266391200000;18.4550064000000;12.7833736800000;19.0766080800000;25.3659816000000;31.6592160000000;19.2271824000000;6.79514880000000;-5.63688480000000;-18.0689184000000;-30.5009520000000;-18.3391800000000;-6.17740800000000;5.98436400000000;0.393809760000000;-5.20060536000000;-10.7911596000000;-16.3855747200000;-21.9761289600000;-14.5979872800000;-7.21984560000000;0.158296080000000;7.53643776000000;1.64859576000000;-4.23924624000000;-10.1270882400000;-16.0149302400000;-7.03066248000000;1.95360528000000;3.37440912000000;14.3856388800000;25.4007295200000;10.0382880000000;-5.32801440000000;-20.6904559200000;-36.0528974400000;-22.7135570400000;-9.37807752000000;3.96126288000000;17.2967424000000;4.18133304000000;7.21598472000000;10.2506364000000;13.2852880800000;9.66378264000000;6.03841632000000;2.41691088000000;3.89562792000000;5.37434496000000;-5.75271120000000;-16.8797673600000;-13.3702274400000;-8.10012624000000;-2.83002504000000;2.44007616000000;7.71017736000000;-2.96129496000000;-13.6366281600000;-13.1617399200000;-12.6907125600000;-12.2158243200000;-9.27769464000000;-6.33956496000000;-3.79138416000000;-1.24320336000000;1.30883832000000;-8.50165776000000;7.49396808000000;23.4934548000000;39.4890806400000;30.2963253600000;21.1035700800000;11.9069539200000;2.71419864000000;-6.47855664000000;-7.51327248000000;-8.54798832000000;-9.58656504000000;-6.98433192000000;0.779897760000000;8.54412744000000;1.07332464000000;-6.39361728000000;-13.8605592000000;-21.3313620000000;-28.7983039200000;-23.9490386400000;-19.1036342400000;-14.2543689600000;0.559827600000000;-17.7561871200000;-15.7485295200000;-13.7370110400000;-11.7254925600000;-13.9995508800000;-16.2736092000000;-18.5438066400000;-20.8178649600000;-19.0997733600000;-17.3816817600000;-15.6597292800000;-13.9416376800000;-12.2235460800000;-2.37058032000000;7.47852456000000;17.3314903200000;27.1805952000000;37.0335609600000;29.9025156000000;22.7753311200000;15.6442857600000;8.51710128000000;1.38605592000000;-5.74112856000000;-3.88018440000000;-2.01924024000000;-0.158296080000000;1.70264808000000;3.56359224000000;4.59058632000000;5.88012024000000;7.16579328000000;8.45532720000000;3.79524504000000;-0.864837120000000;-5.52491928000000;-1.29339480000000;2.93426880000000;7.16579328000000;2.84546856000000;-1.47871704000000;-5.79904176000000;-10.1232273600000;-3.92265408000000;2.27791920000000;8.47849248000000;0.467166480000000;-7.54029864000000;-15.5477637600000;-10.9108468800000;-6.27393000000000;-1.63701312000000;-0.756732480000000;0.119687280000000;0.996107040000000;1.87638768000000;2.75280744000000;3.63308808000000;4.50950784000000;5.38978848000000;6.75654000000000;8.12329152000000;9.49004304000000;10.8606554400000;12.2274069600000;13.5941584800000;16.2350004000000;18.8758423200000;13.7408719200000;8.60590152000000;3.47093112000000;-1.66403928000000;-6.80287056000000;-2.75666832000000;1.28953392000000;5.33959704000000;-5.07319632000000;-15.4859896800000;-25.8987830400000;-18.6094416000000;-11.3201001600000;-4.02689784000000;3.26244360000000;10.5517850400000;17.8411264800000;12.1810764000000;6.51716544000000;0.857115360000000;-4.80293472000000;-10.3587410400000;-15.9106864800000;-21.4626319200000;-12.5594426400000;-3.65239248000000;5.25079680000000;5.52878016000000;5.80676352000000;6.08474688000000;16.2504439200000;10.3664628000000;4.48248168000000;-1.40149944000000;-7.28548056000000;-13.1694616800000;-12.0266412000000;-10.8838207200000;-11.2621869600000;-11.6405532000000;-12.0189194400000;-1.49802144000000;9.02287656000000;19.5437745600000;14.7485616000000;9.95720952000000;5.16199656000000;0.366783600000000;-4.42456848000000;-9.21978144000000;-14.0111335200000;-4.04234136000000;5.92645080000000;15.8952429600000;25.8640351200000;20.1036021600000;14.3431692000000;8.57887536000000;2.81844240000000;-2.94199056000000;-8.70242352000000;-14.4667173600000;-15.4473808800000;-16.4319052800000;-17.4009861600000;-18.3739279200000;-19.3430088000000;-17.5476996000000;-15.7523904000000;-11.1038908800000;-6.45153048000000;-1.80303096000000;2.84932944000000;0.447862080000000;-1.95360528000000;-4.35507264000000;-6.75654000000000;0.814645680000000;8.38969224000000;15.9647388000000;23.5359244800000;31.1109710400000;27.0068556000000;22.8988792800000;18.7947638400000;14.6906484000000;9.87227016000000;5.05775280000000;0.243235440000000;-4.57514280000000;-9.38966016000000;-14.2080384000000;-19.0225557600000;-11.4822571200000;-3.94195848000000;3.59834016000000;11.1347779200000;18.6750765600000;26.2153752000000;18.7715985600000;11.3278219200000;3.88404528000000;-3.55973136000000;-11.0073688800000;-18.4511455200000;-9.48232128000000;-0.513497040000000;8.45532720000000;17.4241514400000;26.3929756800000;19.2194606400000;12.0459456000000;4.87243056000000;-2.30108448000000;-9.47073864000000;-16.6442536800000;-23.8177687200000;-30.9912837600000;-38.1647988000000;-24.9103977600000;-11.6559967200000;1.59840432000000;14.8566662400000;28.1110672800000;23.1614191200000;18.2117709600000;13.2621228000000;12.4745032800000;11.6907446400000;10.9031251200000;1.52890848000000;-7.84530816000000;-1.20845544000000;5.42839728000000;12.0613891200000;18.6982418400000;25.3350945600000;19.8140361600000;14.2929777600000;8.77191936000000;3.25472184000000;-2.26633656000000;-7.78739496000000;-10.4166542400000;-13.0459135200000;-15.6790336800000;-18.3082929600000;-20.9375522400000;-13.6482108000000;-6.35886936000000;-6.26234736000000;-6.16968624000000;-6.07702512000000;-2.88407736000000;0.308870400000000;3.50181816000000;-0.277983360000000;-4.05778488000000;-7.83758640000000;-11.6173879200000;-15.4010503200000;-13.4281406400000;-11.4552309600000;-9.48618216000000;-11.8722060000000;-14.2620907200000;-16.6519754400000;-19.0379992800000;-21.4278840000000;-23.8139078400000;2.03082288000000;27.8755536000000;24.4625356800000;21.0456568800000;17.6287780800000;14.2118992800000;14.2003166400000;14.1887340000000;14.1771513600000;6.81445320000000;-0.552105840000000;-7.91866488000000;-15.2813630400000;-22.6479220800000;-13.7292892800000;-4.80679560000000;4.11569808000000;13.0343308800000;21.9568245600000;17.3816817600000;12.8065389600000;8.22753528000000;3.65239248000000;-0.922750320000000;0.803063040000000;2.52501552000000;4.25082888000000;5.97664224000000;4.63305600000000;3.28560888000000;1.94202264000000;0.594575520000000;-0.752871600000000;-0.196904880000000;0.355200960000000;-4.38209880000000;-9.12325944000000;-13.8605592000000;-18.6017198400000;-23.3390196000000;-28.0801802400000;-10.9919253600000;6.09632952000000;23.1807235200000;19.5708007200000;15.9608779200000;12.3509551200000;12.1038588000000;11.8567624800000;11.6096661600000;7.19281944000000;2.77597272000000;-1.64087400000000;-6.06158160000000;-10.4784283200000;-14.8952750400000;-11.4861180000000;-8.07696096000000;-9.01129392000000;-9.94562688000000;-10.8838207200000;-11.8181536800000;-12.7563475200000;-5.29326648000000;2.16595368000000;9.62903472000000;8.52482304000000;7.42447224000000;6.32412144000000;5.22377064000000;4.86856968000000;4.51722960000000;0.652488720000000;-3.21611304000000;-7.08085392000000;-10.9455948000000;-14.8103356800000;-18.6789374400000;-14.4744391200000;-10.2699408000000;-6.06544248000000;-1.86094416000000;2.34355416000000;6.54805248000000;3.01148640000000;-0.525079680000000;-4.06164576000000;-7.59821184000000;-11.1347779200000;1.94588352000000;15.0304058400000;9.04218096000000;3.05395608000000;-2.93040792000000;-8.91863280000000;-2.72964216000000;3.45548760000000;9.64447824000000;15.8296080000000;22.0185986400000;11.2737696000000;0.528940560000000;-10.2120276000000;-20.9568566400000;-13.8489765600000;-6.74109648000000;0.370644480000000;7.47852456000000;14.5864046400000;8.80666728000000;3.02692992000000;-2.75280744000000;-8.53254480000000;-14.3122821600000;-20.0920195200000;-25.8717568800000;-31.6514942400000;-11.9108148000000;7.82600376000000;27.5666832000000;47.3073626400000;33.3734467200000;19.4395308000000;5.50561488000000;-8.42830104000000;-22.3622169600000;-36.2922720000000;-50.2261879200000;-13.9416376800000;22.3429125600000;18.5399457600000;14.7369789600000;10.9340121600000;7.12718448000000;3.32421768000000;14.0999337600000;24.8795107200000;23.8177687200000;22.7560267200000;21.6904238400000;23.4471242400000;25.2038246400000;17.8681526400000;10.5324806400000;3.20066952000000;-4.13500248000000;-11.4668136000000;-12.1154414400000;-12.7640692800000;-13.4126971200000;-14.0613249600000;-17.6596651200000;-21.2580052800000;-24.8602063200000;-28.4585464800000;-32.0568866400000;-13.9184724000000;4.21608096000000;22.3544952000000;18.1306924800000;13.9068897600000;9.68308704000000;5.45928432000000;13.7485936800000;22.0379030400000;30.3272124000000;24.3389875200000;18.3507626400000;12.3663986400000;6.37817376000000;0.393809760000000;-3.55973136000000;-7.51327248000000;-11.4668136000000;-15.4164938400000;-19.3700349600000;-23.3235760800000;-27.2771172000000;-31.2306583200000;0.741288960000000;32.7132362400000;27.1497081600000;21.5823192000000;16.0149302400000;20.4472204800000;24.8756498400000;29.3040792000000;33.7363694400000;38.1647988000000;26.2462622400000;14.3238648000000;2.40532824000000;-9.51706920000000;-21.4394666400000;-33.3580032000000;-45.2804006400000;-57.1989372000000;-33.6475692000000;-10.0962012000000;13.4551668000000;37.0026739200000;27.4122480000000;17.8218220800000;8.23139616000000;-1.36289064000000;-10.9533165600000;-20.5437424800000;1.81075272000000;24.1613870400000;46.5158822400000;38.4543648000000;30.3928473600000;22.3313299200000;14.2698124800000;6.20829504000000;-1.85322240000000;-9.91473984000000;-17.9762572800000;-26.0377747200000;-34.0992921600000;-42.1608096000000;-50.2223270400000;-42.4503756000000;-34.6784241600000;-26.9064727200000;-19.1306604000000;-15.4666852800000;-11.7988492800000;-8.13487416000000;-4.47089904000000;-3.01148640000000;-1.55207376000000;-0.0926611200000000;1.36675152000000;2.82616416000000;4.28557680000000;3.01148640000000;1.73739600000000;0.463305600000000;-0.810784800000000;-2.08487520000000;3.20839128000000;8.50551864000000;13.8026460000000;19.0997733600000;24.3969007200000;19.4820004800000;14.5671002400000;9.65220000000000;4.73729976000000;-0.177600480000000;-1.86094416000000;-3.54814872000000;-5.23149240000000;-6.91483608000000;-8.60204064000000;-3.40915704000000;1.78372656000000;6.97661016000000;12.1694937600000;8.78736288000000;5.40909288000000;2.03082288000000;-1.35130800000000;-4.72957800000000;-8.11170888000000;-5.54808456000000;-2.98446024000000;-0.424696800000000;-3.17750424000000;-5.93417256000000;-8.69084088000000;-6.61368744000000;-4.53653400000000;-2.45938056000000;-5.31257088000000;-8.16190032000000;-11.0112297600000;-13.8644200800000;-16.7137495200000;-13.3509230400000;-9.98809656000000;-6.62140920000000;-3.25858272000000;0.104243760000000;3.46707024000000;0.486470880000000;-2.49026760000000;-5.47086696000000;-7.87233432000000;-10.2738016800000;-12.6752690400000;-15.0767364000000;-17.4782037600000;-14.0497423200000;-10.6174200000000;-7.18895856000000;-3.76049712000000;-0.332035680000000;5.14655304000000;10.6251417600000;16.1037304800000;10.8567945600000;5.60985864000000;0.362922720000000;-4.88015232000000;-10.1270882400000;-6.52488720000000;-2.91882528000000;0.683375760000000;4.28943768000000;7.89163872000000;11.4938397600000;15.0999016800000;9.42826896000000;3.75663624000000;-1.91499648000000;-7.58662920000000;-13.2582619200000;-7.93024752000000;-2.60223312000000;2.72578128000000;8.05379568000000;13.3818100800000;10.2815234400000;7.18123680000000;4.08095016000000;0.980663520000000;0.243235440000000;-0.494192640000000;-1.23162072000000;-1.96904880000000;-3.85701912000000;-5.74498944000000;-3.05395608000000;-0.359061840000000;2.33583240000000;-1.32042096000000;-4.97281344000000;-8.62906680000000;-12.2814592800000;-15.9377126400000;-10.4514021600000;-4.96895256000000;0.517357920000000;5.99980752000000;11.4861180000000;16.9685676000000;13.9454985600000;10.9185686400000;7.89163872000000;4.86470880000000;1.83777888000000;-1.18529016000000;-4.21222008000000;-3.79910592000000;-3.38213088000000;-2.96515584000000;-2.55204168000000;-4.76432592000000;-6.97661016000000;-9.18889440000000;-11.4011786400000;-13.6134628800000;-10.7486899200000;-7.88391696000000;-5.01914400000000;13.1849052000000;2.42463264000000;2.39760648000000;2.37444120000000;2.35127592000000;2.32424976000000;2.30108448000000;2.27791920000000;2.25089304000000;2.22772776000000;2.20456248000000;2.17753632000000;2.15437104000000;2.13120576000000;2.10417960000000;2.08101432000000;2.05398816000000;2.03082288000000;2.00765760000000;1.98063144000000;1.95746616000000;1.93430088000000;1.90727472000000;1.88410944000000;1.86094416000000;1.83391800000000;1.81075272000000;1.78758744000000;1.76056128000000;1.73739600000000;1.71423072000000;1.68720456000000;1.66403928000000;1.64087400000000;1.61384784000000;1.59068256000000;1.56751728000000;1.54049112000000;1.51732584000000;1.49416056000000;1.46713440000000;1.44396912000000;1.42080384000000;1.39377768000000;1.37061240000000;1.34744712000000;1.32042096000000;1.29725568000000;1.27409040000000;1.24706424000000;1.22389896000000;1.20073368000000;1.17370752000000;1.15054224000000;1.12737696000000;1.10035080000000;1.07718552000000;1.05402024000000;1.02699408000000;1.00382880000000;0.980663520000000;0.953637360000000;0.930472080000000;0.907306800000000;0.880280640000000;0.857115360000000;0.833950080000000;0.806923920000000;0.783758640000000;0.760593360000000;0.733567200000000;0.710401920000000;0.687236640000000;0.660210480000000;0.637045200000000;0.610019040000000;0.586853760000000;0.563688480000000;0.536662320000000;0.513497040000000;0.490331760000000;0.463305600000000;0.440140320000000;0.416975040000000;0.389948880000000;0.366783600000000;0.343618320000000;0.316592160000000;0.293426880000000;0.270261600000000;0.243235440000000;0.220070160000000;0.196904880000000;0.169878720000000;0.146713440000000;0.123548160000000;0.0965220000000000;0.0733567200000000;0.0501914400000000;0.0231652800000000;0;0;];
T=0.02;
N=length(ag);
t=0:T:(N-1)*T;
a1 =-475.38 ;
a2 = -0.5404;
x0=0;
v=0;
for k=1:N;
x(k)=x0+T*v
v(k)=a1*x(k)+a2*v(k)-ag(k)
end

Accepted Answer

Walter Roberson
Walter Roberson on 28 Apr 2012
You initialize v=0 so that makes v length 1. In the first iteration of the for loop, k=1, you use v(1) to calculate a value that is assigned to v(1). On the second iteration of the for loop,, k=2, you try to use v(2) to calculate v(2) but v(2) does not exist yet.
Caution: if you did manage to create v(2) and so on, then your statement
x(k)=x0+T*v
would be using the vector v, and so would calculate a vector result and would attempt to store that vector into the single numeric array element x(k) . That is going to fail.
  1 Comment
Bob
Bob on 28 Apr 2012
I think I got it now Thank you.
this is what I did.
for k=1:N;
x(k)=x0+T*v0;
x0=x(k)
v(k)=v0+T*(a1*x0+a2*v0-ag(k));
v0=v(k)
end

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!