Using function as arguments

조회 수: 5 (최근 30일)
Antonio
Antonio 2013년 9월 11일
I've just started to program and i've had some problems in passing function as arguments using Matlab. I've to implementate Lagrange Algorithm for interpolation. C1 and C2 are vectors that represent points to interpolate coordinates.
My main problem is that I don't know how to explain in my f1 definition that temp1 and temp2 are not variables but values determined on every for loop (for i and j). I think the code remaining part could be almost correct.
function [ ] = lagrange( C1,C2 )
n=length(C1);
f2=inline('');
g=inline('');
for i=1:n
temp0=C2(i);
temp1=C1(i);
for j=1:n
if (i~=j)
temp2=C1(j);
temp3=C2(j);
f1=inline('(x-temp2/(temp1-temp2)','x','temp1','temp2');
f2=f2.*f1
end
g=g+temp0*f2;
end
end
%plot g
end

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 11일
편집: Azzi Abdelmalek 2013년 9월 11일
You can use
f=@yourfunction
Example
f=@cos
f(1)
Look at
doc function_handle
doc str2func
  댓글 수: 3
Antonio
Antonio 2013년 9월 11일
편집: Antonio 2013년 9월 11일
I think i've not well explained my question because I think I don't need string control but it's first time for me that i program and i can not even explain well my problems. Having for example values for a and b, variables for each loop, i'd like to write a function only in x, for example, considering the second element of each vector i=2.
a=[0 0.25 0.5 1];
b=[1 5 3 7];
f(x)=x+a(i)+b(i);
I'd like that, at the end, for example at Loop i=2 the result'd be f(x)=x+5.25;
Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 11일
This is not clear

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by