# Question:implementing least square method in Maple

## Question:implementing least square method in Maple

Maple 14

Hi,

I am really stuck in one more bit thats the least square problem.  leastsquare1.mw

Hope someone can help me to solve this bit and get me out of this problem :(.

Least square method is successfuly implemented in MATLAB code (WRITTEN IN THIS MESSAGE AT THE END), but I couldn't do it in Maple.  I am having problem in the for loop section,  I am attaching Matlab code and  maple code (for correction) pls.

Your help will be really appreciated.

Best Regards

A.Q

Soton

Matlab code as I cannot upload the file:

close all;

clear all;

clc

nr=1500;

Do=190e-3;

Ds=120.8e-3;

la=76e-3;

Qs=36;

p=2;

alpha_p=0.556;

lm=6.3e-3;

g=0.5e-3;

mur=1.045;

Br = 1.16;

Rs=Ds*0.5;

bo=4*Rs*pi/180;

do=0.6e-3;

dslot=17.2e-3;

Mtype='R';

Nlambda=64;

NB=301;

N=128;

Rs=Ds*0.5;

Rm=Rs-g;

Rr=Rs-g-lm;

R=Rs-g*0.5;

Rp1=Rr;

Rp2=Rs;

Rp3=Rs;

Rp4=Rs;

thetas=2*pi/Qs;

alphao=2*asin(0.5*bo/Rs);

theta1=thetas/2-alphao/2;

theta2=theta1+alphao;

clear j

boprime=theta2-theta1;

gprime=log(Rp2/Rp1);

bCM=(boprime/2/gprime+sqrt((boprime/2/gprime)^2+1))^2;

aCM=1/bCM;

clear z_realz_imag

z_imag=j*[0:thetas/N:thetas];

z_real(1:length(z_imag))=log(R);

z=z_real+z_imag;

s=exp(z);

FCM=inline('j*gprime/pi*(log((1+s)/(1-s))-log((b+s)/(b-s))-2*(b-1)/sqrt(b)*atan(s/sqrt(b)))+log(Rp2)+j*theta2',...'s','b','gprime','Rp2','theta2');

%START OPTIMIZATION

w0=[0.01*aCM];

%initial values

for i=1:length(z)

res=@(w0)(FCM(sqrt((w0-bCM)/(w0-aCM)),bCM,gprime,Rp2,theta2)-z(i));

[w(i),resnorm,residual,exitflag]=lsqnonlin(res,w0,[],[]);

k(i)=exp(j*gprime/pi*log(w(i))+log(Rp2)+j*thetas/2);

lambda(i)=k(i)*(w(i)-1)/(w(i)-aCM)^(1/2)/(w(i)-bCM)^(1/2)/s(i);

w0=w(i);

end

lambda=[conj(lambda(N/2+1:-1:1)) lambda(2:N/2)];

figure;plot(real(lambda));grid

figure;plot(imag(lambda));grid

﻿