diary on %Numeerinen analyysi s2012, harjoitus 4 % %Matlabin diary-komento on kätevä. Toivon, että osaisin käyttää sitä järkevästi. %diaryllä saa talteen käytettyjä komentoja, mutta se pitäisi laittaa päälle silloin, %kun tietää, että kirjoittaa jotain oleellista % %Tehtävä (i) 2*rand(5,1)-1 ans = 0.6294 0.8116 -0.7460 0.8268 0.2647 %komennolla saadaan siis 5x1 matriisi, jonka alkiot ovat tasajakautuneita satunnaislukuja %onko lukujen odotusarvo nolla?%haluan esitellä Matlabin komentoa mean, joka laskee matriisin keskiarvot sarakkeittain %jotta komennon toiminta tulisi selväksi, haluan käyttää "satunnaista" matriisia, jossa on kokonaislukuja %tällaisia saa magic-komennolla, joka antaa "taikaneliöitä" a=magic(2).^2 a = 1 9 16 4 mean(a) ans = 8.5000 6.5000 mean(a') ans = 5 10 mean(mean(a)) ans = 7.5000 a=2*rand(2)-1 a = 0.9298 0.9412 -0.6848 0.9143 %tutkitaan odotusarvoa suurella matriisilla a=2*rand(1000)-1; %<- ; estää matriisia tulostumasta komentoikkunaan mean(mean(a)) ans = 6.4277e-04 %siis ylläolevaa muotoa olevien satunnaislukujen odotusarvo on nolla diary off %Tehtävä (ii) 2*randn(5,1)-1 ans = 0.5898 -2.4217 -4.2022 0.3026 1.2343 %komennon pitäisi antaa normaalijakautuneita satunnaislukuja joitten odotusarvo on -1 ja hajonta on 2 %kokeillaan a=2*randn(1000)-1; mean(mean(a)) ans = -1.0013 %siis odotusarvo on -1 b=mean(mean((a-(-1)).^2)); b b = 3.9953 sqrt(b) ans = 1.9988 %siis hajonta on 2 diary off %Komento norm a=magic(2) a = 1 3 4 2 norm(a) ans = 5.1167 ans^2 ans = 26.1803 norm(a,1) ans = 5 diary off %Komento norm a=magic(2).^2 a = 1 9 16 4 norm(a) ans = 16.8908 norm(a,1) ans = 17 %saatiin suurin sarakesumma alkioiden itseisarvoista norm(a,2) ans = 16.8908 norm(a,inf) ans = 20 %saatiin suurin rivisumma alkioiden itseisarvoista norm(a,3) {Error using norm The only matrix norms available are 1, 2, inf, and 'fro'. } norm(a,fro) {Undefined function or variable 'fro'. } norm(a,'fro') ans = 18.8149 %fro on myös joku matriisinormi diary off %Piirtäminen x=linspace(-1,2);y=x.^3-4*x-1;plot(x,y) t=linspace(0,2*pi);plot(cos(t),sin(t)) %ympyrä on soikea. se saadaan pyöreäksi seuraavalla tavalla axis equal %liitetään a-kohdan käyrä samaan kuvaan hold Current plot held x=linspace(-1,2);y=x.^3-4*x-1;plot(x,y) %tallennetaan tämä kuva jpg- ja eps-muodossa print -djpeg 'kayrajaympyra.jpg' print -depsc 'kayrajaympyra.eps' %kokeillaan myös komentoa print print %tämä aiheutti sen, että yliopiston kirjaston tulostin tulosti kuvan alle kymmenessä sekunnissa %kuvan saa kiinni ilman hiirtä komennolla close %close close plot(x,y,'*') print -djpeg 'kayrapisteina.jpg' %halutaan piirtää kompleksinen vektori v, mistä sellainen saadaan? a=1+i a = 1.0000 + 1.0000i v=2*rand(5,1)-1+i*(2*randn(5,1)-1) v = 0.9744 - 4.6329i -0.9814 + 0.0338i 0.4193 + 0.0733i -0.1142 + 3.1779i -0.4990 - 1.7964i plot(v,'*') %muutetaan huvin vuoksi kuvan asemointia axis([-10 10 -10 10]) print -djpeg 'kompleksinenv.jpg' diary off %Pari kuvankäsittelykomentoa x=linspace(-1,2);y=x.^3-4*x-1;plot(x,y) hold Current plot held t=linspace(0,2*pi);plot(cos(t),sin(t)) axis equal t=linspace(0,2*pi);plot(1+cos(t)/2,1+sin(t)/2,'r') t=linspace(0,2*pi);plot(2+cos(t)/3,2+sin(t)/3,'k') title('Käyrä ja ympyröitä') xlabel('Tämä on x-akseli') ylabel('Tämä on y-akseli') %lisätään tekstiä vielä keskelle kuvaa [a,b]=ginput a = 2.0260 b = -0.9011 text(a,b,'käyrän pää') print -djpeg 'kuvankasittelykomentoja.jpg' diary off 888888888888 88 88 88 88 ,d ,d88 88 88 88 888888 88 ,adPPYba, 88,dPPYba, MM88MMM 88 88 a8P_____88 88P' "8a 88 88 88 8PP""""""" 88 88 88 88 88 "8b, ,aa 88 88 88, 888 88 88 `"Ybbd8"' 88 88 "Y888 888 88 p=[1,2,3,4] p = 1 2 3 4 a=roots(p) a = -1.6506 -0.1747 + 1.5469i -0.1747 - 1.5469i poly(a) ans = 1.0000 2.0000 3.0000 4.0000 %siis roots antaa polynomin nollakohdat ja poly tekee nollakohtien perusteella polynomin %polynomilla p ja 2*p on kuitenkin samat nollakohdat diary off roots(2*p) ans = -1.6506 -0.1747 + 1.5469i -0.1747 - 1.5469i a=roots(p) a = -1.6506 -0.1747 + 1.5469i -0.1747 - 1.5469i real(a) ans = -1.6506 -0.1747 -0.1747 imag(a) ans = 0 1.5469 -1.5469 abs(a) ans = 1.6506 1.5567 1.5567 %siis real, imag ja abs antavat kompleksilukujen reaaliosan, imaginaariosan ja modulin diary off for k=1:10000 p=round(randn(1,5)); r=roots(p); a(k)=numel(unique(r)); end mean(a) ans = 3.1734 max(a) ans = 4 min(a) ans = 0 for k=0:4 eval(['b(' num2str(k) ')=numel(a==(' num2str(k) '));']) end {Subscript indices must either be real positive integers or logicals. } for k=0:4 eval(['b(' num2str(k+1) ')=numel(a==(' num2str(k) '));']) end b b = 10000 10000 10000 10000 10000 for k=0:4 a=[0 0 0 1 1 0 0] find(a==0) end a = 0 0 0 1 1 0 0 ans = 1 2 3 6 7 a = 0 0 0 1 1 0 0 ans = 1 2 3 6 7 a = 0 0 0 1 1 0 0 ans = 1 2 3 6 7 a = 0 0 0 1 1 0 0 ans = 1 2 3 6 7 a = 0 0 0 1 1 0 0 ans = 1 2 3 6 7 clear clc diary on p=[1,2,3,4] p = 1 2 3 4 x=linspace(-10,10);y=polyval(p,x);plot(x,y) axis([-2 2 -10 10]) %polynomien arvoja voi siis laskea komennolla polyval %polynomeja voi derivoida komennolla polyder q=polyder(p) q = 3 4 3 diary off for k=1:10000 p=randn(1,5); r=roots(p); a(k)=numel(unique(r)); end mean(a) ans = 4 max(a),min(a) ans = 4 ans = 4 %arvottiin 10000 4. asteen polynomia. niillä on kaikilla 4 erillistä juurta. %entä, jos arvottaisiin kokonaislukukertoimisia polynomeja? for k=1:10000 p=round(randn(1,5)); r=roots(p); a(k)=numel(unique(r)); end mean(a) ans = 3.2026 max(a),min(a) ans = 4 ans = 0 diary off %nyt osa juurista on useampikertaisia %polynomeilla on keskimäärin mean(a) ans = 3.2026 %erillistä juurta diary off b=numel(find(a==1)) b = 779 for k=0:4 eval(['b(k+1)=numel(find(a==1)) eval(['b(k+1)=numel(find(a==1)) | {Error: A MATLAB string constant is not terminated properly. } for k=0:4 eval(['b(' num2str(k+1) ')=numel(find(a==' num2str(1) '));']) end b b = 779 779 779 779 779 for k=0:4 eval(['b(' num2str(k+1) ')=numel(find(a==' num2str(k) '));']) end b b = 203 779 1127 2571 5320 %Matlabin mukaan 5320 polynomilla arvotuista 10000 kokonaislukukertoimisesta polynomista on neljä erillistä juurta diary off diary on 888888888888 88 ad888888b, 88 88 ,d d8" "88 88 88 88 a8P 88 ,adPPYba, 88,dPPYba, MM88MMM ,d8P" 88 a8P_____88 88P' "8a 88 a8P" 88 8PP""""""" 88 88 88 a8P' 88 "8b, ,aa 88 88 88, 888 d8" 88 `"Ybbd8"' 88 88 "Y888 888 88888888888 %kirjoitetaan luentorungon lemman 2.5 s.23 kaava muistiin kaava=string('P=p(2:end);r1=max(1,sum(abs(P)));r2=max(1+sum(abs(P).^2))^0.5;r3=1+max(abs(P));r=min([r1,r2,r3]);') {Warning: string is obsolete and will be discontinued. Use char instead.} kaava = P=p(2:end);r1=max(1,sum(abs(P)));r2=max(1+sum(abs(P).^2))^0.5;r3=1+max(abs(P));r=min([r1,r2,r3]); p=randn(1,5); P=p(2:end);r1=max(1,sum(abs(P)));r2=max(1+sum(abs(P).^2))^0.5;r3=1+max(abs(P));r=min([r1,r2,r3]); r1 r1 = 4.5381 r2 r2 = 3.3544 r3 r3 = 4.0349 r r = 3.3544 %kaava näyttäisi toimivan R=max(abs(roots(p))); R R = 2.4908 R/r ans = 0.7425 for k=1:10000 r=[1,randn(5,1)]; P=p(2:end);r1=max(1,sum(abs(P)));r2=max(1+sum(abs(P).^2))^0.5;r3=1+max(abs(P));r=min([r1,r2,r3]); R=max(abs(roots(p))); a(k)=R/r; end {Error using horzcat CAT arguments dimensions are not consistent. } for k=1:10000 r=[1,randn(1,5)]; P=p(2:end);r1=max(1,sum(abs(P)));r2=max(1+sum(abs(P).^2))^0.5;r3=1+max(abs(P));r=min([r1,r2,r3]); R=max(abs(roots(p))); a(k)=R/r; end mean(a) ans = 0.7425 %näillä kertoimilla 0,7425-säteinen kiekko näyttäisi riittävän diary off 888888888888 88 ad888888b, 88 88 ,d d8" "88 88 88 88 a8P 88 ,adPPYba, 88,dPPYba, MM88MMM aad8" 88 a8P_____88 88P' "8a 88 ""Y8, 88 8PP""""""" 88 88 88 "8b 88 "8b, ,aa 88 88 88, 888 Y8, a88 88 `"Ybbd8"' 88 88 "Y888 888 "Y888888P' %funktio on tiedostossa h4t3f.m x=linspace(-40,40,10^5);y=h4t3f(x);plot(x,y) >> nolla=0*x; >> hold on >> plot(x,nolla) >> title('Harjoitus 4, tehtävä 3, funktio f') >> fzero(@h4t3f,0) ans = -0.2684 >> plot(0,ans,'*') >> fzero(@h4t3f,3) ans = 3.2567 >> plot(3,ans,'ko') >> fzero(@h4t3f,-2) ans = -3.0032 >> plot(ans,0,'kx') >> print -djpeg 'h4t3f.jpg' %piirtämisessä tuli pari ajatusvirhettä, mutta se ei haitanne 888888888888 88 ,d8 88 88 ,d ,d888 88 88 88 ,d8" 88 88 ,adPPYba, 88,dPPYba, MM88MMM ,d8" 88 88 a8P_____88 88P' "8a 88 ,d8" 88 88 8PP""""""" 88 88 88 8888888888888 88 "8b, ,aa 88 88 88, 888 88 88 `"Ybbd8"' 88 88 "Y888 888 88 %montako ratkaisua on? %ehkä 2? %arvotaan satunnaisia alkuarvoja ja ratkaistaan systeemi niillä for k=1:30 b(k,:)=fsolve(@h4t4,randn(1,2)); end %TÄSTÄ TULEE RUMAN NÄKÖISTÄ. MUISTA, ETTÄ Ctrl+c pysäyttää loputtoman tai "loputtoman" silmukan. b=round(b*10^3)/10^3; %pyöristetään b:tä unique(b,'rows') ans = -0.0980 0.2190 -0.0980 0.2200 0.1210 0.2710 %siis 20 satunnaisella alkuarvolla löytyi 2 eri ratkaisua (pätee 0.2190=0.2200) 888888888888 88 8888888888 88 88 ,d 88 88 88 88 88 ____ 88 ,adPPYba, 88,dPPYba, MM88MMM 88a8PPPP8b, 88 a8P_____88 88P' "8a 88 PP" `8b 88 8PP""""""" 88 88 88 d8 88 "8b, ,aa 88 88 88, 888 Y8a a8P 88 `"Ybbd8"' 88 88 "Y888 888 "Y88888P" opt0=optimset('fsolve') opt0 = Display: 'final' %TÄMÄ MUUTTUU MaxFunEvals: [] MaxIter: 400 TolFun: 1.0000e-06 TolX: 1.0000e-06 FunValCheck: 'off' OutputFcn: [] PlotFcns: [] ActiveConstrTol: [] Algorithm: 'trust-region-dogleg' AlwaysHonorConstraints: [] BranchStrategy: [] DerivativeCheck: 'off' Diagnostics: 'off' DiffMaxChange: Inf DiffMinChange: 0 FinDiffRelStep: [] FinDiffType: 'forward' GoalsExactAchieve: [] GradConstr: [] GradObj: [] HessFcn: [] Hessian: [] HessMult: [] HessPattern: [] HessUpdate: [] InitialHessType: [] InitialHessMatrix: [] InitBarrierParam: [] InitTrustRegionRadius: [] Jacobian: 'off' JacobMult: [] JacobPattern: 'sparse(ones(jrows,jcols))' LargeScale: [] LineSearchType: [] MaxNodes: [] MaxPCGIter: 'max(1,floor(numberofvariables/2))' MaxProjCGIter: [] MaxRLPIter: [] MaxSQPIter: [] MaxTime: [] MeritFunction: [] MinAbsMax: [] NodeDisplayInterval: [] NodeSearchStrategy: [] NoStopIfFlatInfeas: [] ObjectiveLimit: [] PhaseOneTotalScaling: [] Preconditioner: [] PrecondBandWidth: Inf RelLineSrchBnd: [] RelLineSrchBndDuration: [] ScaleProblem: 'none' Simplex: [] SubproblemAlgorithm: [] TolCon: [] TolConSQP: [] TolGradCon: [] TolPCG: 0.1000 TolProjCG: [] TolProjCGAbs: [] TolRLPFun: [] TolXInteger: [] TypicalX: 'ones(numberofvariables,1)' UseParallel: [] opt=optimset(opt0,'Display','iter') opt = Display: 'iter' %TÄMÄ MUUTTUI MaxFunEvals: [] MaxIter: 400 TolFun: 1.0000e-06 TolX: 1.0000e-06 FunValCheck: 'off' OutputFcn: [] PlotFcns: [] ActiveConstrTol: [] Algorithm: 'trust-region-dogleg' AlwaysHonorConstraints: [] BranchStrategy: [] DerivativeCheck: 'off' Diagnostics: 'off' DiffMaxChange: Inf DiffMinChange: 0 FinDiffRelStep: [] FinDiffType: 'forward' GoalsExactAchieve: [] GradConstr: [] GradObj: [] HessFcn: [] Hessian: [] HessMult: [] HessPattern: [] HessUpdate: [] InitialHessType: [] InitialHessMatrix: [] InitBarrierParam: [] InitTrustRegionRadius: [] Jacobian: 'off' JacobMult: [] JacobPattern: 'sparse(ones(jrows,jcols))' LargeScale: [] LineSearchType: [] MaxNodes: [] MaxPCGIter: 'max(1,floor(numberofvariables/2))' MaxProjCGIter: [] MaxRLPIter: [] MaxSQPIter: [] MaxTime: [] MeritFunction: [] MinAbsMax: [] NodeDisplayInterval: [] NodeSearchStrategy: [] NoStopIfFlatInfeas: [] ObjectiveLimit: [] PhaseOneTotalScaling: [] Preconditioner: [] PrecondBandWidth: Inf RelLineSrchBnd: [] RelLineSrchBndDuration: [] ScaleProblem: 'none' Simplex: [] SubproblemAlgorithm: [] TolCon: [] TolConSQP: [] TolGradCon: [] TolPCG: 0.1000 TolProjCG: [] TolProjCGAbs: [] TolRLPFun: [] TolXInteger: [] TypicalX: 'ones(numberofvariables,1)' UseParallel: [] diary off opt0=optimset('fsolve'); opt1=optimset(opt0,'Display','iter'); opt2=optimset(opt0,'Display','iter','Jacobian','on'); %halutaan katsoa fsolve(@h4t4,[1 1],optx) jollakin optx fsolve(@h4t4,[1 1],opt1) Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 3 22.8551 38.6 1 1 6 1.03383 0.79291 4.57 1 2 9 0.0571063 0.242225 0.601 1.98 3 12 0.00230394 0.101844 0.0749 1.98 4 15 2.88428e-05 0.0339037 0.00666 1.98 5 18 1.24008e-08 0.00487822 0.000133 1.98 6 21 2.71356e-15 0.000105482 6.22e-08 1.98 Equation solved. fsolve completed because the vector of function values is near zero as measured by the selected value of the function tolerance, and the problem appears regular as measured by the gradient. ans = 0.1212 0.2711 %toimi fsolve(@h4t4,[1 1],opt2) Error using h4t4 Too many output arguments. Error in fsolve (line 250) [fuser,JAC] = feval(funfcn{3},x,varargin{:}); Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue. %ei toiminut opt3=optimset(opt0,'MaxIter',1); fsolve(@h4t4,[1 1],opt3) Solver stopped prematurely. fsolve stopped because it exceeded the iteration limit, options.MaxIter = 1 (the selected value). ans = 0.4802 0.4012 >> h4t4(ans) ans = 0.9922 -0.2222 opt4=optimset(opt0,'MaxIter',3); fsolve(@h4t4,[1 1],opt4) Solver stopped prematurely. fsolve stopped because it exceeded the iteration limit, options.MaxIter = 3 (the selected value). ans = 0.1591 0.2802 h4t4(ans) ans = 0.0480 -0.0014 %jo kolmella iteraatiolla saadaan jonkinlainen ratkaisu 888888888888 88 ad8888ba, 88 88 ,d 8P' "Y8 88 88 88 d8 88 ,adPPYba, 88,dPPYba, MM88MMM 88,dd888bb, 88 a8P_____88 88P' "8a 88 88P' `8b 88 8PP""""""" 88 88 88 88 d8 88 "8b, ,aa 88 88 88, 888 88a a8P 88 `"Ybbd8"' 88 88 "Y888 888 "Y88888P" %funktio on Matlabissa nimellä h4t6.m >> fsolve(@h4t6,[0 0 0]) Equation solved. fsolve completed because the vector of function values is near zero as measured by the default value of the function tolerance, and the problem appears regular as measured by the gradient. ans = -1.6090 -1.2019 0.7941 >> %näin saatiin yksi ratkaisu >> fsolve(@h4t6,[1 1 1]) No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the default value of the function tolerance. ans = 0.4121 2.4719 2.3808 >> %funktio on Matlabissa nimellä h4t6.m >> fsolve(@h4t6,[0 0 0]) Equation solved. fsolve completed because the vector of function values is near zero as measured by the default value of the function tolerance, and the problem appears regular as measured by the gradient. ans = -1.6090 -1.2019 0.7941 >> %funktio on Matlabissa nimellä h4t6.m >> ra1=fsolve(@h4t6,[0 0 0]) Equation solved. fsolve completed because the vector of function values is near zero as measured by the default value of the function tolerance, and the problem appears regular as measured by the gradient. ra1 = -1.6090 -1.2019 0.7941 >> %Matlab sanoo: "Equation solved.", joten tämän pitäisi olla oikea ratkaisu >> %Tarkistus: >> h4t6(ra1) ans = 1.0e-06 * 0.0523 -0.2779 0.1180 >> %Nähdään, että h4t6 on pisteessä ra1 melkein 0, joten saatiin oikea ratkaisu >> ra2=fsolve(@h4t6,[1 1 1]) No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the default value of the function tolerance. ra2 = 0.4121 2.4719 2.3808 >> %Matlab sanoo: "No solution found." Siis lopputulos ei ole ratkaisu, Matlab vain lopetti iteroinnin siihen >> h4t6(ra2) ans = 5.5086 -0.1407 0.1478 >> %h4t6 ei ole 0 pisteessä ra2, joten ra2 ei ole ratkaisu >> ra3=fsolve(@h4t6,[-1 2 -4]) Equation solved. fsolve completed because the vector of function values is near zero as measured by the default value of the function tolerance, and the problem appears regular as measured by the gradient. ra3 = -1.6090 -1.2019 0.7941 >> ra1-ra3 ans = 1.0e-06 * -0.0770 0.2348 0.1735 >> %nyt saatiin siis sama ratkaisu, kuin aiemmin >> %alkuarvoja on ihmisen vaikea muuttaa satunnaisesti itse >> %käytetään for-silmukkaa for k=1:10 x=10*randn(1,3); a(k,:)=fsolve(@h4t6,x); end %TÄSTÄ TULEE RUMAA JÄLKEÄ. Muista Ctrl+c, jolla saat silmukan poikki. %alla esimerkkiajon antama vastaus a a = -1.9665 -0.1009 1.0144 -1.9665 -0.1009 1.0144 0.4121 2.4719 2.3808 -1.9665 -0.1009 1.0144 -1.9665 -0.1009 1.0144 0.4121 2.4719 2.3808 -1.6090 -1.2019 0.7941 -1.6090 -1.2019 0.7941 1.5237 -3.0136 1.6677 -1.9665 -0.1009 1.0144 b=round(a*10^4)/10^4 %pyöristetään a:n alkiot, että voidaan käyttää unique-komentoa b = -1.9665 -0.1009 1.0144 -1.9665 -0.1009 1.0144 0.4121 2.4719 2.3808 -1.9665 -0.1009 1.0144 -1.9665 -0.1009 1.0144 0.4121 2.4719 2.3808 -1.6090 -1.2019 0.7941 -1.6090 -1.2019 0.7941 1.5237 -3.0136 1.6677 -1.9665 -0.1009 1.0144 c=unique(b,'rows') c = -1.9665 -0.1009 1.0144 -1.6090 -1.2019 0.7941 0.4121 2.4719 2.3808 1.5237 -3.0136 1.6677 %löydettiin muka neljä ratkaisua %tarkistus: h4t6b(a) ans = 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 5.5086 -0.1407 0.1478 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 5.5086 -0.1407 0.1478 0.0000 0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.0000 h4t6b(c) ans = -0.0001 0.0001 -0.0002 0.0006 -0.0000 -0.0000 5.5087 -0.1400 0.1480 -0.0001 0.0000 -0.0004 %nähdään, että c:n kolmas rivi ei ole ratkaisu! c=[c(1:2,:);c(4,:)] c = -1.9665 -0.1009 1.0144 -1.6090 -1.2019 0.7941 1.5237 -3.0136 1.6677 %kun kolmas rivi jätetään pois, jäävät jäljelle oikeat ratkaisut, joita löydettiin nyt kolme Lisäys, h4t5: 'Jacobian','on' - toimii kun on kyseessä Moodlen funktion fu3: " function [y,dy]=fu3(x) y(1)=x(1)^2+x(2)^2-4; y(2)=x(1)*x(2)-1; dy(1,1)=2*x(1); dy(1,2)=2*x(2); dy(2,1)=x(2); dy(2,2)=x(1); " kaltainen funktio. Tällöin fsolvelle voidaan kertoa, että funktio antaa Jacobin matriisin valmiiksi. Näin fsolven ei tarvitse laskea sitä itse katso alla huomautukset "%ENEMMÄN LASKEMISTA" ja "%VÄHEMMÄN LASKEMISTA" opt0=optimset('fsolve'); >> opt5=optimset(opt0,'Display','iter','Jacobian','on'); >> fsolve(@fu3,[1 1]) No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the default value of the function tolerance. ans = 1.3416 1.3416 >> fsolve(@fu3,[0 0]) No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the default value of the function tolerance. ans = 0 0 >> opt6=optimset(opt0,'Display','iter'); >> fsolve(@fu3,[0 0],opt6) Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 3 17 0 1 No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the selected value of the function tolerance. ans = 0 0 >> fsolve(@fu3,3,opt6) Attempted to access x(2); index out of bounds because numel(x)=1. Error in fu3 (line 2) y(1)=x(1)^2+x(2)^2-4; Error in fsolve (line 241) fuser = feval(funfcn{3},x,varargin{:}); Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue. >> fsolve(@fu3,[2 2],opt6) Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 3 25 22 1 1 6 1.25753 0.777817 2.19 1 2 9 0.800592 0.147517 0.0732 1.94 3 12 0.8 0.00571735 0.00011 1.94 4 15 0.8 8.61337e-06 2.49e-10 1.94 No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the selected value of the function tolerance. ans = 1.3416 1.3416 >> fsolve(@fu3,[7 7],opt6) Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 3 * 11140 1.65e+03 1 1 6 * 7145.19 1 1.19e+03 1 2 9 * 1744.91 2.5 423 2.5 %ENEMMÄN LASKEMISTA * 3 12 * 91.4847 2.91848 52.4 6.25 4 15 3.60035 1.22342 5.97 7.3 5 18 0.815094 0.331493 0.374 7.3 6 21 0.800001 0.0285258 0.00273 7.3 7 24 0.8 0.00021441 1.54e-07 7.3 No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the selected value of the function tolerance. ans = 1.3416 1.3416 >> fsolve(@fu3,[7 -3],opt6) Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 3 3400 822 1 1 6 1856.94 1 530 1 2 9 212.56 2.5 113 2.5 3 12 10.8714 1.72035 15.8 6.25 4 15 0.373843 0.739527 2.21 6.25 5 18 0.00291976 0.219841 0.176 6.25 6 21 3.87853e-07 0.0236015 0.00201 6.25 7 24 7.51383e-15 0.000278438 2.79e-07 6.25 Equation solved. fsolve completed because the vector of function values is near zero as measured by the selected value of the function tolerance, and the problem appears regular as measured by the gradient. ans = 1.9319 0.5176 >> fsolve(@fu3,[7 -3],opt5) Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 1 * 3400 822 1 %VÄHEMMÄN LASKEMISTA * 1 2 * 1856.94 1 530 1 2 3 * 212.56 2.5 113 2.5 3 4 * 10.8714 1.72035 15.8 6.25 4 5 0.373843 0.739527 2.21 6.25 5 6 0.00291976 0.219841 0.176 6.25 6 7 3.87853e-07 0.0236015 0.00201 6.25 7 8 7.51313e-15 0.000278437 2.79e-07 6.25 Equation solved. fsolve completed because the vector of function values is near zero as measured by the selected value of the function tolerance, and the problem appears regular as measured by the gradient. ans = 1.9319 0.5176