Error using ==> inline.subsref at 14 Not enough inputs to inline function.

조회 수: 1 (최근 30일)
hi guys
Please if anyone can help me with my code. i face an error that says
**Error using ==> inline.subsref at 14
Not enough inputs to inline function.
x1=-4; x2=5;
F=inline('4*(sqrt(x1^2 + (10-x2)^2 )-10)^2 + 0.5*(sqrt(x1^2 + (10+x2)^2 )-10)^2 -5*(x1+x2)','x1','x2');
for j=1:200
s1=-dF1;
s2=-dF2;% dF1,dF2 can be found
x3=x1+s1*h;
x4=x2+s2*h;
%the problem is i want to use inline function as function of (h).
g=4*(sqrt(x1^3 + (10-x4)^2 )-10)^2 + 0.5*(sqrt(x3^2 + (10+x4)^2 )-10)^2 -5*(x3+x4).
f=inline('g','h');
% later in my loop i will use f(a) where a is known but i always got
*** Error using ==> inline.subsref at 14
Not enough inputs to inline function.
What I need is f as function in h so i can work with.

채택된 답변

Walter Roberson
Walter Roberson 2012년 3월 18일
Anonymous functions are easier.
F = @(x1, x2) 4*(sqrt(x1^2 + (10-x2)^2 )-10)^2 + 0.5*(sqrt(x1^2 + (10+x2)^2 )-10)^2 -5*(x1+x2); %you do not appear to use F!
g = @(x1, x2, h) 4*(sqrt(x1^3 + (10-(x2+s2*h))^2 )-10)^2 + 0.5*(sqrt((x1+s1*h)^2 + (10+(x2+s2*h))^2 )-10)^2 -5*((x1+s1*h)+(x2+s2*h));
x1 = -4; %are these really constants??
x2 = 5; %are these really constants??
for j = 1 : 200
s1 = -dF1; %is this differential? You cannot differentiate an inline function or an anonymous function
s2 = -dF2; %differentiating what?
f = @(h) g(x1, x2, h);
end
  댓글 수: 2
Abdulaziz
Abdulaziz 2012년 3월 18일
I appreciate your time.
x1,x2 are changing each loop.
dF1: I just differentiate F separately, outside the program.
I will send you all the program. do not wary about all the steps. the problem occurred because of f=inline('g','h'). I just want to make f as a function of h which i need to find the minimum of f(h), but i can not write h separately I should substitute x3 and x4 which are function of x1,h and x2,h. respectively.
The code is********************
x1=-4; x2=5;
F=inline('4*(sqrt(x1^2 + (10-x2)^2 )-10)^2 + 0.5*(sqrt(x1^2 + (10+x2)^2 )-10)^2 -5*(x1+x2)','x1','x2');
E_n=100; E_t=10^-3;Tol=10^-10;
for j=1:200
dF1=(8*(sqrt(x1^2 + (10-x2)^2) - 10) * x1 )/sqrt(x1^2 + (10-x2)^2) +((1*(sqrt(x1^2 + (10+x2)^2) - 10) * x1 )/sqrt(x1^2 + (10+x2)^2)) - 5 ;
dF2=((8*(sqrt(x1^2 + (10-x2)^2) - 10)*(x2-10)) / (sqrt(x1^2 + (10-x2)^2))) + (((sqrt(x1^2 + (10+x2)^2) - 10)*(10+x2))/(sqrt(x1^2 + (10+x2)^2))) - 5 ;
s1=-dF1;
s2=-dF2;
x3=x1+s1*h;
x4=x2+s2*h;
% now i want to set g as function of h
g=4*(sqrt(x3^2 + (10-x4)^2 )-10)^2 + 0.5*(sqrt(x3^2 + (10+x4)^2)-10)^2 -5*(x3+x2);
f=inline('g','h');
% Finding the bounds on the minimum of the function
%values of the initial bounds [a,b]=[0.0,0.1]
a(1)=0.0;
b(1)=0.1;
r=0.61803;
GSR=1.61803; %Golden Section Ratio = (r/r-1)
c(1)=r*a(1)+(1-r)*b(1); % c is a point between [a,b]
for i=1:100
if if f(a(i))>f(c(i)) && f(b(i))>f(c(i))
% This means there is a minimum value of the function f in the interval [a,b]
break;
else
%since the slope is negative we will shift the interval to the right by
%using the previous values of 'c' and 'b' and the Golden Section Ratio GSR
a(i+1)=c(i); c(i+1)=b(i);
%b can be determined from {(b-c/c-a)=(r/r-1)=GSR, then
b(i+1)=c(i+1)*(1+GSR)-(GSR*a(i+1));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Reduction of original interval using the golden section algorithm.
a(1)=a(i); b(1)=b(i);
c(1)=r*a(1)+(1-r)*b(1);
d(1)=(1-r)*a(1)+r*b(1);% c and d are points between [a,b]
for n=1:100
if f(c(n))<=f(d(n))
a(n+1)=a(n);
b(n+1)=d(n);
d(n+1)=c(n);
c(n+1)=r*a(n+1)+(1-r)*b(n+1);
else
if f(c(n))>f(d(n))
a(n+1)=c(n);
b(n+1)=b(n);
c(n+1)=d(n);
d(n+1)=(1-r)*a(n+1)+r*b(n+1);
end
end
E_n=abs((b(n+1)-a(n+1))/(b(1)-a(1)));
if E_n < E_t;
break
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%cubic plunomyal fit to the points obtained at the last iteration
X1=a(n+1);X2=c(n+1);X3=d(n+1);X4=b(n+1);
q1=X3^3*(X2-X1)-X2^3*(X3-X1)+X1^3*(X3-X2);
q2=X4^3*(X2-X1)-X2^3*(X4-X1)+X1^3*(X4-X2);
q3=(X3-X2)*(X2-X1)*(X3-X1);
q4=(X4-X2)*(X2-X1)*(X4-X1);
q5=f(X3)*(X2-X1)-f(X2)*(X3-X1)+f(X1)*(X3-X2);
q6=f(X4)*(X2-X1)-f(X2)*(X4-X1)+f(X1)*(X4-X2);
a3=(q3*q6-q4*q5)/(q2*q3-q1*q4);
a2=(q5-a3*q1)/q3;
a1=((f(X2)-f(X1))/(X2-X1))-(a3*((X2^3-X1^3)/(X2-X1)))-a2*(X1+X2);
a0=f(X1)-a1*X1-a2*X1^2-a3*X1^3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% finding the minimum
delta=a2^2-(3*a1*a3);
XI=(sqrt(delta)-a2)/(3*a3); XII=(-a2-sqrt(delta))/(3*a3);
if f(XI)<= f(XII)
h=XI;
else
h=XII;
end
x3=x1+s1*h;
x4=x2+s2*h;% we already used this in lines 11,12 for g=f(h)
if abs(F(x3,x4)-F(x1,x2)) <= 10^-10
break;
end
if j==5
break;
end
x1=x3;
x2=x4;
end
disp('f(x1,x2) x1 x2 iteration');
m=[F(x1,x2),x1,x2,j];
disp(m);
Abdulaziz
Abdulaziz 2012년 3월 19일
HI I really appreciate your help it incredibly works.
I hope I ask in the first day because I worked all two days long to figure it out finally i decided to write to your website.
Thaaaaaaaaaank yoooooo

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by