assignment problem in a for loop
이전 댓글 표시
Hello people, I have an assignment problem and I don't know how to solve.
clc;
clear;
close all;
%% Time structure
dt=0.01; %sampling time
%% System structure
A = [1.02 -0.1
0.1 0.98];
B = [0.5 0
0.05 0.5];
G = [0.3 0
0 0.3];
C = eye(2,2);
Qf = [50 0
0 50];
%% MPC parameters
Q = eye(2);
R = eye(2)*50;
N = 5;
%% Building block matrices
I = eye(2,2);
q = 2;
m = 2;
Qbar = blkdiag(Q,Q,Q,Q,Qf,R,R,R,R);
Fq = blkdiag(sqrt(Q),sqrt(Q),sqrt(Q),sqrt(Q),sqrt(Q),sqrt(Qf),sqrt(R),sqrt(R),sqrt(R),sqrt(R),sqrt(R));
Gxx = zeros(2*N , size(A,1));
for i = 1:N
Gxx(q*i-q+1:q*i , :) = A^i;
end
Gxu = zeros(2*N , 2*N);
for i = 1:N
for j = 1:i
Gxu(q*i-q+1:q*i , m*j-m+1:m*j) = A^(i-j)*(B);
end
end
Gxw = zeros(2*N , 2*N);
for i = 1:N
for j = 1:i
Gxw(q*i-q+1:q*i , m*j-m+1:m*j) = A^(i-j)*(I);
end
end
%% Mean of u obtained from solver result
ubar1 = [-0.000581629157055178,-0.000581629156609152,-0.000581629281438909,-0.000581629281440897,-0.000581629281439613,-0.000581629281441646,-0.000581629281439253,-0.000581629281441243,-0.000581629283745490,-0.000581629284144206,131.400927231750,0,-43.5899689825575,-87.3447946140090,-0.121677240258015,-0.227961850128840,-0.0869533824650604,-0.130839304907035,-0.00960202231921041,-0.00960984528042930,0.0454701361380619,0.0690829738668646;-0.000528160807600932,-0.000528160806858417,-0.000528161014656819,-0.000528161014660146,-0.000528161014657446,-0.000528161014660808,-0.000528161014657155,-0.000528161014660429,-0.000528161018496181,-0.000528161019159948,218.736767442156,0,-72.5618189454592,-145.398065579508,-0.202155519406871,-0.379025814694336,-0.144353434705365,-0.217349689091955,-0.0155478255728175,-0.0155653199308984,0.0761167937761455,0.115615840165787;-0.000464850276767520,-0.000464850276767507,-0.000464850276766848,-0.000464850276766868,-0.000464850276767458,-0.000464850276767449,-0.000464850276767172,-0.000464850276767166,-0.000464850276767130,-0.000464850276767136,-0.00464850251156834,0,0,0,0,0,0,0,0,0,0,0;-0.000422890131101226,-0.000422890131101213,-0.000422890131100429,-0.000422890131100414,-0.000422890131100958,-0.000422890131100997,-0.000422890131100708,-0.000422890131100690,-0.000422890131100695,-0.000422890131100693,-0.00422890107641037,0,0,0,0,0,0,0,0,0,0,0;-0.000348309691892651,-0.000348309691892663,-0.000348309691892179,-0.000348309691892155,-0.000348309691892598,-0.000348309691892604,-0.000348309691892381,-0.000348309691892382,-0.000348309691892379,-0.000348309691892380,-0.00348309672703811,0,0,0,0,0,0,0,0,0,0,0;-0.000317448440512582,-0.000317448440512563,-0.000317448440511949,-0.000317448440511982,-0.000317448440512401,-0.000317448440512368,-0.000317448440512166,-0.000317448440512180,-0.000317448440512168,-0.000317448440512169,0,-0.00317448422922303,0,0,0,0,0,0,0,0,0,0;-0.000231995660577032,-0.000231995660577031,-0.000231995660576716,-0.000231995660576710,-0.000231995660577010,-0.000231995660577012,-0.000231995660576852,-0.000231995660576864,-0.000231995660576856,-0.000231995660576843,-0.00231995647779437,0,0,0,0,0,0,0,0,0,0,0;-0.000211826109957114,-0.000211826109957123,-0.000211826109956741,-0.000211826109956731,-0.000211826109957007,-0.000211826109957004,-0.000211826109956862,-0.000211826109956874,-0.000211826109956863,-0.000211826109956857,-0.00211826098199697,0,0,0,0,0,0,0,0,0,0,0;-0.000115896358074462,-0.000115896358074466,-0.000115896358074303,-0.000115896358074301,-0.000115896358074452,-0.000115896358074453,-0.000115896358074381,-0.000115896358074372,-0.000115896358074371,-0.000115896358074375,-0.00115896351700854,0,0,0,0,0,0,0,0,0,0,0;-0.000106013267225743,-0.000106013267225713,-0.000106013267225495,-0.000106013267225514,-0.000106013267225653,-0.000106013267225657,-0.000106013267225582,-0.000106013267225581,-0.000106013267225585,-0.000106013267225582,-0.00106013261349873,0,0,0,0,0,0,0,0,0,0,0];
ubar2 = [-0.000581629151442759,-0.000581629150976575,-0.000581629281437736,-0.000581629281439817,-0.000581629281438418,-0.000581629281440531,-0.000581629281438100,-0.000581629281440181,-0.000581629283848340,-0.000581629284265049,65.9612196789050,0,0.0163528663340642,-0.0115687495095834,-21.9613299789049,-43.9177482522026,-0.0351465169397885,-0.0573268811696319,-0.00848969500416555,-0.0139375019482108,0.00936976354254012,0.0126783667337945;-0.000528160798780068,-0.000528160798005902,-0.000528161014654947,-0.000528161014658418,-0.000528161014655624,-0.000528161014659124,-0.000528161014655291,-0.000528161014658731,-0.000528161018657842,-0.000528161019349869,109.538144585391,0,0.0273450560748362,-0.0190569345422484,-36.4695717310723,-72.9307300429299,-0.0578364398062356,-0.0947345049761504,-0.0136866268276673,-0.0227458754979369,0.0159240204457714,0.0215664414794175;-0.000464850148258548,-0.000464850147797768,-0.000464850276739902,-0.000464850276742009,-0.000464850276740514,-0.000464850276742547,-0.000464850276740215,-0.000464850276742327,-0.000464850279122408,-0.000464850279534250,0,65.1932395076792,0.0163002305454987,-0.0113383082218456,-21.7054853369752,-43.4060306759143,-0.0346084968557704,-0.0565336465142243,-0.00832334193286985,-0.0137285737033249,0.00927132212395355,0.0125888175165625;-0.000422889917178537,-0.000422889916411515,-0.000422890131055573,-0.000422890131059036,-0.000422890131056160,-0.000422890131059569,-0.000422890131055833,-0.000422890131059312,-0.000422890135021385,-0.000422890135706981,0,108.524418821749,0.0273655844303290,-0.0185747423894637,-36.1317741553739,-72.2557401916522,-0.0572547780465873,-0.0938525774969211,-0.0134878862596612,-0.0224802718950870,0.0158385014160028,0.0213127948735551;-0.000348309691892901,-0.000348309691892894,-0.000348309691892164,-0.000348309691892170,-0.000348309691892627,-0.000348309691892591,-0.000348309691892341,-0.000348309691892409,-0.000348309691892398,-0.000348309691892359,-0.00348309672711165,0,0,0,0,0,0,0,0,0,0,0;-0.000317448440512921,-0.000317448440512929,-0.000317448440511979,-0.000317448440511974,-0.000317448440512384,-0.000317448440512391,-0.000317448440512184,-0.000317448440512162,-0.000317448440512158,-0.000317448440512165,0,0,0,-0.00317448422917531,0,0,0,0,0,0,0,0;-0.000231995660577191,-0.000231995660577196,-0.000231995660576725,-0.000231995660576713,-0.000231995660577007,-0.000231995660577007,-0.000231995660576840,-0.000231995660576861,-0.000231995660576864,-0.000231995660576847,-0.00231995647789305,0,0,0,0,0,0,0,0,0,0,0;-0.000211826109957367,-0.000211826109957373,-0.000211826109956736,-0.000211826109956738,-0.000211826109957001,-0.000211826109956983,-0.000211826109956865,-0.000211826109956888,-0.000211826109956864,-0.000211826109956847,-0.00211826098199371,0,0,0,0,0,0,0,0,0,0,0;-0.000115896358074550,-0.000115896358074549,-0.000115896358074301,-0.000115896358074296,-0.000115896358074455,-0.000115896358074451,-0.000115896358074373,-0.000115896358074376,-0.000115896358074373,-0.000115896358074374,-0.00115896351693273,0,0,0,0,0,0,0,0,0,0,0;-0.000106013267225860,-0.000106013267225836,-0.000106013267225499,-0.000106013267225518,-0.000106013267225656,-0.000106013267225653,-0.000106013267225577,-0.000106013267225580,-0.000106013267225585,-0.000106013267225581,0,0,0,0,0,0,0,0,0,0,-0.00106013261343957,0];
ubar3 = [-0.000581629145856014,-0.000581629145369818,-0.000581629281436549,-0.000581629281438723,-0.000581629281437267,-0.000581629281439437,-0.000581629281436938,-0.000581629281439131,-0.000581629283950715,-0.000581629284385333,0;-0.000528160790044915,-0.000528160789239457,-0.000528161014653141,-0.000528161014656719,-0.000528161014653785,-0.000528161014657422,-0.000528161014653464,-0.000528161014657056,-0.000528161018817937,-0.000528161019537960,0;-0.000464850142736338,-0.000464850142255738,-0.000464850276738761,-0.000464850276740947,-0.000464850276739355,-0.000464850276741506,-0.000464850276739047,-0.000464850276741207,-0.000464850279223623,-0.000464850279653178,0;-0.000422889908523924,-0.000422889907725896,-0.000422890131053791,-0.000422890131057371,-0.000422890131054291,-0.000422890131057855,-0.000422890131054043,-0.000422890131057684,-0.000422890135180004,-0.000422890135893310,0;-0.000348309559422107,-0.000348309558947132,-0.000348309691864400,-0.000348309691866537,-0.000348309691864830,-0.000348309691866954,-0.000348309691864606,-0.000348309691866753,-0.000348309694320250,-0.000348309694744800,43.0436491684950;-0.000317448219995202,-0.000317448219204558,-0.000317448440465745,-0.000317448440469294,-0.000317448440466157,-0.000317448440469703,-0.000317448440465946,-0.000317448440469510,-0.000317448444553722,-0.000317448445260454,71.6525962622234;-0.000231995660577381,-0.000231995660577364,-0.000231995660576710,-0.000231995660576727,-0.000231995660577002,-0.000231995660576996,-0.000231995660576870,-0.000231995660576862,-0.000231995660576844,-0.000231995660576842,-0.00231995647785187;-0.000211826109957630,-0.000211826109957637,-0.000211826109956729,-0.000211826109956725,-0.000211826109957006,-0.000211826109957009,-0.000211826109956874,-0.000211826109956867,-0.000211826109956849,-0.000211826109956848,-0.00211826098201988;-0.000115896358074637,-0.000115896358074632,-0.000115896358074308,-0.000115896358074307,-0.000115896358074440,-0.000115896358074445,-0.000115896358074386,-0.000115896358074375,-0.000115896358074369,-0.000115896358074373,-0.00115896351684206;-0.000106013267225990,-0.000106013267225966,-0.000106013267225503,-0.000106013267225519,-0.000106013267225655,-0.000106013267225648,-0.000106013267225582,-0.000106013267225588,-0.000106013267225578,-0.000106013267225575,-0.00106013261328094];
ubar = [ubar1 ubar2 ubar3];
%% Kalman gain obtaind from solver result
K1 = [44.0635380945976,0.0144792342297621,0.00899088120540256,-0.0124382672869247,-0.0223441170985152,-14.6762348967978,-29.3464412433947,-0.0100789965127368,-0.0183297272704375,-0.00148141843763935,-0.00558644338784057;72.9973128950942,0.0244479819082575,0.0153090895709597,-0.0201779270976766,-0.0365491331966745,-24.3128391828889,-48.6159239970137,-0.0161442327881510,-0.0300272845581608,-0.00200941463662751,-0.00878084663970879;43.5507054837716,0.0144200653254241,0.00898094935076691,-0.0121928816277802,-0.0219638690448114,-14.5053129127818,-29.0047439028058,-0.00984404045116668,-0.0180157931805780,-0.00131719338963689,-0.00536440740900765;72.3219199166351,0.0241936868346877,0.0151660575668501,-0.0200027039117206,-0.0362572464667515,-24.0877901457276,-48.1657843934554,-0.0158107503157870,-0.0295520299572297,-0.00180944623703407,-0.00850184559544692;0,0.0143473042089087,0.00895446326796452,-0.0119783647393111,-0.0216170886824211,-14.3363020536890,-28.6668482923762,-0.00959862061015196,-0.0176755508650136,-0.00120934496620102,-0.00520471650514216;0,0.0240875170155106,0.0151235909454769,-0.0197116546030324,-0.0358156872907119,-23.8645602682563,-47.7198464280140,-0.0155440294572866,-0.0292536758614791,-0.00181252602425768,-0.00843758446433569;0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,0,0,0];
K2 = [-0.000581629140520318,-0.000581629140014967,-0.000581629281435421,-0.000581629281437707,-0.000581629281436155,-0.000581629281438415,-0.000581629281435776,-0.000581629281438066,-0.000581629284048542,-0.000581629284500241,33.1028257072545,0,0.0112490119602310,0.0112473556934346,-0.00292777573905732,-0.00436087360384590,-0.00834074815108921,-0.0130520322384191,-11.0275646016738,-22.0540143645686,-0.00657393380603968,-0.0144146452715128;-0.000528160781767472,-0.000528160780932333,-0.000528161014651370,-0.000528161014655111,-0.000528161014652074,-0.000528161014655837,-0.000528161014651727,-0.000528161014655437,-0.000528161018969639,-0.000528161019716197,54.7082630863979,0,0.0189286284954669,0.0189103757597692,-0.00454113156244581,-0.00689384922596755,-0.0135015600147111,-0.0210767833743903,-18.2242737481433,-36.4471019107356,-0.0105525110348784,-0.0235426487928676;-0.000464850137462193,-0.000464850136962711,-0.000464850276737639,-0.000464850276739886,-0.000464850276738278,-0.000464850276740523,-0.000464850276737923,-0.000464850276740185,-0.000464850279320284,-0.000464850279766736,0,32.7179153179613,0.0112345673342110,0.0112163959609964,-0.00278319683941357,-0.00418577411828557,-0.00814214715349870,-0.0128358008736269,-10.8992135703394,-21.7973468908096,-0.00639021248503765,-0.0141171908707592;-0.000422889900322715,-0.000422889899495272,-0.000422890131052072,-0.000422890131055772,-0.000422890131052567,-0.000422890131056314,-0.000422890131052341,-0.000422890131056058,-0.000422890135330305,-0.000422890136069922,0,54.2017664448784,0.0189102797440820,0.0188790780054832,-0.00431688326309377,-0.00666606535534156,-0.0132115671865431,-0.0207785265821466,-18.0555781694114,-36.1098118292599,-0.0103030776224307,-0.0231185845624800;-0.000348309554208820,-0.000348309553715187,-0.000348309691863313,-0.000348309691865506,-0.000348309691863733,-0.000348309691865966,-0.000348309691863537,-0.000348309691865716,-0.000348309694415777,-0.000348309694857077,32.3368807977701,0,0.0112181867735355,0.0112050146547400,-0.00264624138886693,-0.00403942571585900,-0.00793936627616469,-0.0125083755937647,-10.7721706065646,-21.5434283256527,-0.00620675334625093,-0.0138480011120754;-0.000317448211869598,-0.000317448211049784,-0.000317448440464021,-0.000317448440467722,-0.000317448440464449,-0.000317448440468131,-0.000317448440464258,-0.000317448440467932,-0.000317448444702636,-0.000317448445435430,53.7005126816206,0,0.0188306154290914,0.0187983500141845,-0.00418479906990456,-0.00650422788317405,-0.0129787758516639,-0.0206037169145362,-17.8884600942730,-35.7756277679274,-0.0100996300998782,-0.0228571188161968;-0.000231995524496290,-0.000231995524008378,-0.000231995660548183,-0.000231995660550392,-0.000231995660548475,-0.000231995660550666,-0.000231995660548338,-0.000231995660550513,-0.000231995663070879,-0.000231995663507021,31.9604088939063,0,0.0112066324330570,0.0112147539670611,-0.00249739118314646,-0.00392555353680225,-0.00771411199815516,-0.0122641592061716,-10.6466425028912,-21.2924601288557,-0.00600142224565183,-0.0136449665955420;-0.000211825883430054,-0.000211825882617863,-0.000211826109909245,-0.000211826109912894,-0.000211826109909523,-0.000211826109913165,-0.000211826109909373,-0.000211826109913023,-0.000211826114108563,-0.000211826114834567,53.2033600166952,0,0.0188448086146954,0.0187787624459470,-0.00397036507284986,-0.00630757558107225,-0.0127006896994598,-0.0202050826654821,-17.7227112959222,-35.4443641534376,-0.00985352249305821,-0.0229891634134136;-0.000115896358074734,-0.000115896358074724,-0.000115896358074296,-0.000115896358074303,-0.000115896358074448,-0.000115896358074456,-0.000115896358074386,-0.000115896358074361,-0.000115896358074363,-0.000115896358074379,-0.00115896351690545,0,0,0,0,0,0,0,0,0,0,0;-0.000106013267226122,-0.000106013267226104,-0.000106013267225503,-0.000106013267225519,-0.000106013267225655,-0.000106013267225651,-0.000106013267225582,-0.000106013267225583,-0.000106013267225577,-0.000106013267225575,0,-0.00106013261346298,0,0,0,0,0,0,0,0,0,0];
K3 = [-0.000581629135519974,-0.000581629134996698,-0.000581629281434357,-0.000581629281436717,-0.000581629281435096,-0.000581629281437487,-0.000581629281434781,-0.000581629281437079,-0.000581629284140153,-0.000581629284607911,26.5256579509458,0,0.00835038284765902,0.00947832280545638,-0.000533997427724830,-6.68585671312291e-06,-0.00405488169836614,-0.00564807655900998,-0.00595031101092397,-0.0109888523403815,-8.84050296644894,-17.6817277853915;-0.000528160774083449,-0.000528160773220748,-0.000528161014649796,-0.000528161014653655,-0.000528161014650437,-0.000528161014654326,-0.000528161014650111,-0.000528161014653958,-0.000528161019110479,-0.000528161019881656,0,43.7323827136632,0.0140321836641676,0.0159035734195373,-0.000542426950676478,0.000364240646642466,-0.00625922274696526,-0.00888142880463039,-0.00945508644359899,-0.0175429498368847,-14.5745638190296,-29.1508198297976;-0.000464850132519463,-0.000464850132002244,-0.000464850276736625,-0.000464850276738974,-0.000464850276737208,-0.000464850276739529,-0.000464850276736892,-0.000464850276739237,-0.000464850279410886,-0.000464850279873170,0,26.2170978854204,0.00839666410543007,0.00946862846858551,-0.000444547095147061,8.91388874535797e-05,-0.00392615549751531,-0.00547020218953360,-0.00581631085589865,-0.0107350123263100,-8.73755516972260,-17.4757534214187;-0.000422889892709482,-0.000422889891854738,-0.000422890131050451,-0.000422890131054295,-0.000422890131050992,-0.000422890131054839,-0.000422890131050725,-0.000422890131054571,-0.000422890135469843,-0.000422890136233858,43.3276919263775,0,0.0140211697873117,0.0160142083983239,-0.000366651051102402,0.000478818303015614,-0.00607735037249898,-0.00860533574328808,-0.00911470174798128,-0.0173708258234057,-14.4396727514416,-28.8812274072871;-0.000348309549323091,-0.000348309548811909,-0.000348309691862276,-0.000348309691864573,-0.000348309691862713,-0.000348309691865016,-0.000348309691862500,-0.000348309691864786,-0.000348309694505334,-0.000348309694962264,0,25.9122873075480,0.00837341318636336,0.00954768611622805,-0.000271138675591953,0.000155350652316167,-0.00376835543820737,-0.00533741699325959,-0.00563027057910208,-0.0105847090624864,-8.63581190567657,-17.2724430575202;-0.000317448204326399,-0.000317448203479583,-0.000317448440462472,-0.000317448440466267,-0.000317448440462869,-0.000317448440466661,-0.000317448440462651,-0.000317448440466486,-0.000317448444840901,-0.000317448445597841,0,42.9271217709031,0.0140008448155133,0.0158117464386425,-0.000281578836956038,0.000561272672078999,-0.00608381201958536,-0.00834542194250592,-0.00891479934815123,-0.0171139707435029,-14.3061071810531,-28.6138233546433;-0.000231995519666879,-0.000231995519161643,-0.000231995660547176,-0.000231995660549459,-0.000231995660547456,-0.000231995660549719,-0.000231995660547326,-0.000231995660549600,-0.000231995663159393,-0.000231995663610998,0,25.6105655006555,0.00825393995232175,0.00935996007256233,-0.000324169279509413,0.000194898147037028,-0.00372020476659378,-0.00501585548608801,-0.00530996212076040,-0.0101735252828744,-8.53504170390693,-17.0711088341814;-0.000211825875956376,-0.000211825875117378,-0.000211826109907680,-0.000211826109911447,-0.000211826109907945,-0.000211826109911717,-0.000211826109907806,-0.000211826109911582,-0.000211826114245537,-0.000211826114995488,42.5305249978802,0,0.0140376983366531,0.0159144637481751,-0.000164166896230518,0.000626851134912908,-0.00600537149991451,-0.00843348581306537,-0.00900589897935376,-0.0169609079999982,-14.1736314302266,-28.3490210102000;-0.000115896218804857,-0.000115896218305517,-0.000115896358045113,-0.000115896358047351,-0.000115896358045253,-0.000115896358047496,-0.000115896358045170,-0.000115896358047424,-0.000115896360626858,-0.000115896361073200,0,25.3125511016011,0.00836257814113481,0.00941757713773058,-0.000126986021959314,0.000379968576672813,-0.00350929619294082,-0.00494546128359014,-0.00522984039418426,-0.0100579904300554,-8.43566564435439,-16.8723349700185;-0.000106013035390354,-0.000106013034559113,-0.000106013267176911,-0.000106013267180650,-0.000106013267177042,-0.000106013267180788,-0.000106013267176985,-0.000106013267180711,-0.000106013271474569,-0.000106013272217585,0,42.1367074550257,0.0140593145017628,0.0158875194539718,-6.90156975402793e-05,0.000796806145614419,-0.00560810907779284,-0.00825150493231501,-0.00872792090512335,-0.0167696079526953,-14.0424571232569,-28.0866279454555];
%K = [K1 K2 K3];
K = [44.0635380945976,0.0144792342297621,0.00899088120540256,-0.0124382672869247,-0.0223441170985152,-14.6762348967978,-29.3464412433947,-0.0100789965127368,-0.0183297272704375,-0.00148141843763935;72.9973128950942,0.0244479819082575,0.0153090895709597,-0.0201779270976766,-0.0365491331966745,-24.3128391828889,-48.6159239970137,-0.0161442327881510,-0.0300272845581608,-0.00200941463662751;43.5507054837716,0.0144200653254241,0.00898094935076691,-0.0121928816277802,-0.0219638690448114,-14.5053129127818,-29.0047439028058,-0.00984404045116668,-0.0180157931805780,-0.00131719338963689;72.3219199166351,0.0241936868346877,0.0151660575668501,-0.0200027039117206,-0.0362572464667515,-24.0877901457276,-48.1657843934554,-0.0158107503157870,-0.0295520299572297,-0.00180944623703407;0,0.0143473042089087,0.00895446326796452,-0.0119783647393111,-0.0216170886824211,-14.3363020536890,-28.6668482923762,-0.00959862061015196,-0.0176755508650136,-0.00120934496620102;0,0.0240875170155106,0.0151235909454769,-0.0197116546030324,-0.0358156872907119,-23.8645602682563,-47.7198464280140,-0.0155440294572866,-0.0292536758614791,-0.00181252602425768;0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,0,0];
%% Constructing mean of state
xbar0 = ones(2,55);
xbar = (Gxx*xbar0)+(Gxu*ubar);
%% Main loop
x0 = ones(2,55);
x(:,1) = ones(10,55);
for k = 2:55
x(:,k) = Gxx*x0 + Gxu*(K*(x(:,k-1)-xbar)+ubar);
end
plot(x)
댓글 수: 16
Given that x is not defined previously, what do you expect this line to do?:
x(:,1) = ones(10,55);
Possibly you meant to write this (but your allocation error indicates that there might be deeper confusion...):
x = ones(10,55);
When I display the size of the calculation results like this
for k = 2:55
tmp = Gxx*x0 + Gxu*(K*(x(:,k-1)-xbar)+ubar);
size(tmp) % show size of results
end
this is what it shows me:
ans =
10 55
ans =
10 55
ans =
10 55
ans =
10 55
ans =
10 55
... lots more times the same
ans =
10 55
ans =
10 55
ans =
10 55
ans =
10 55
ans =
10 55
In your code you try to allocate these results to x using:
x(:,k) = ...
which refers to exactly 10 elements. How do you expect to fit 550 elements into 10 elements?
Ali Esmaeilpour
2019년 6월 15일
편집: Ali Esmaeilpour
2019년 6월 15일
Ali Esmaeilpour
2019년 6월 15일
Ali Esmaeilpour
2019년 6월 15일
madhan ravi
2019년 6월 15일
what is the purpose of x(:.1) =... ?
Ali Esmaeilpour
2019년 6월 15일
madhan ravi
2019년 6월 15일
MATLAB doesn’t allow zero indexing k-1 leads to zero when k is 1
Ali Esmaeilpour
2019년 6월 15일
Ali Esmaeilpour
2019년 6월 15일
Stephen23
2019년 6월 15일
You can preallocate a correctly sized array and use suitable indexing, e.g.:
x = ones(10,55,10);
for k = 2:10
x(:,:,k) = ...
end
Ali Esmaeilpour
2019년 6월 15일
Stephen23
2019년 6월 15일
"working fine!"
Sure... except that you replace the entire contents of array x on every iteration, thus discarding the results from every iteration except the last one. I doubt that that is what you are after.
Guillaume
2019년 6월 15일
Stephen has already shown you how to allocate enough elements as a 3D array. It's very simple, if you're going to generate 5500 numbers, you need an array with 5500 elements, not 550. How you allocate that is up to you, as a 3D array as Stephen showed, as a 10x550 2D array, as a 100x55 array, etc. but you need 5500 elements.
Note that x(:, :) = ... when x is 2D is exactly the same as x = .... The indexing does nothing useful and just shows that you're confused.
Ali Esmaeilpour
2019년 6월 15일
"question is clear x is 10*55 and it has a formula:"
Sure, that much is clear.
But what is not clear is what you expect to be stored in the output array, because
a) your code is buggy, so it does not help us understand what you are trying to do, and
b) you have not explained the output array to us, even though this thread of >ten comments discusses this one topic extensively.
So, here is an important question for you: Given that on each of ten iterations you generate a matrix with size 10x55, and you apparently want them all stored, what size do you expect the final output array to be? Please answer this exact question.
Note that ten iterations of a 10x55 matrix means a total of 5500 elements.
Ali Esmaeilpour
2019년 6월 16일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
