You are on page 1of 5

Matlab trials

Aug. 25
x = fsolve(fun,x0) default options
x = fsolve(fun,x0,options)
>> sol=fsolve('[sqrt(x(1))+log(x(2))+0.5*cos(x(3));x(2)*log(4*x(1))-2*x(3)exp(x(3))+1;4*x(1)+x(2)*sin(x(3))-1]',[1/3 1/3 pi/6])
Optimization terminated: first-order optimality is less than options.TolFun.
sol =
x= 0.2500 y= 0.3679 z= -0.0000
Note: sol=fsolve(*f1;f2;f3+,*1/3 1/3 pi/6+)
F1 should be typed not as matrix but as an equation
>> [x,fval]=fsolve('[sqrt(x(1))+log(x(2))+0.5*cos(x(3));x(2)*log(4*x(1))-2*x(3)exp(x(3))+1;4*x(1)+x(2)*sin(x(3))-1]',[1/3 1/3 pi/6])
Optimization terminated: first-order optimality is less than options.TolFun. %this means you have
solved the problem
x=
0.2500 0.3679 -0.0000
fval =
1.0e-011 *
-0.1056
0.0049
0.0081
Fval used to solve values of x,y,z and a solution as well
At guess(1/3 1/3 pi)
x=
0.2500 0.3679 -0.0000
fval =
1.0e-007 *
-0.2339

0.0535
0.0042
- For optimization, use partial derivatives
-solve solves symbolically
-fsolve-solves numerically
>> sol=solve('sqrt(x)+log(y)+0.5*cos(z)','y*log(4*x)-2*z-exp(z)+1','4*x+y*sin(z)-1')
sol =
x: [1x1 sym]
y: [1x1 sym]
z: [1x1 sym]
>> sol.x
ans =
.75704775407906177085398261002915e-5
>> sol.y
ans =
.99998469090641783565100240764180
>> sol.z
ans =
-4.7069166713345252268070623747392
- solve means there are multiple solutions, any value can be assigned
- fsolve is able to find one solution according to your guess
>> sqrt(A.x)+log(A.y)+0.5*cos(A.z)
ans =
.122e-31
>> A.y*log(4*A.x)-2*A.z-exp(A.z)+1
ans =
.30e-30
>> 4*A.x+A.y*sin(A.z)-1

ans =
0.
->> A=solve('x^2+y^2-4','x^3+y^3')

%A is symbolic

A=
x: [6x1 sym]
y: [6x1 sym]
>> sol.x
ans =
.75704775407906177085398261002915e-5
>> sol.y
ans =
.99998469090641783565100240764180
>> X=eval(A.x)
X=
-1.4142
1.4142
-1.7321 - 1.0000i
-1.7321 + 1.0000i
1.7321 + 1.0000i
1.7321 - 1.0000i
>> Y=eval(A.y)
Y=
1.4142

(1st solution, sqrt 2)

-1.4142
-1.7321 + 1.0000i
-1.7321 - 1.0000i
1.7321 - 1.0000i

1.7321 + 1.0000i
- but there can be real solutions
- to convert symbolic to numeric use eval function
---- ./ - element by element operation
Solution to PT2:
>> x=[0:0.2:0.8 0.895]'
x=
0
0.2000
0.4000
0.6000
0.8000
0.8950
>> r=[2.24e-3 1.52e-3 9.63e-4 5.34e-4 2.18e-4 1.03e-4]'
r=
0.0022
0.0015
0.0010
0.0005
0.0002
0.0001
>> f=1./r
f=
1.0e+003 *
0.4464
0.6579
1.0384

1.8727
4.5872
9.7087
>> int=trapz(x,f)
int =
1.8962e+003
>> w=11.175*int
w=
2.1190e+004

You might also like