# How do I pass coefficients to function handle as an array or list?

10 views (last 30 days)

Show older comments

Travis Briles
on 30 Jun 2021

Commented: Joseph Cheng
on 30 Jun 2021

I have a situation where the same multi-variable argument is used in several different functions defined with function handles. I would like to define the arguments as an array so that I don't have to keep copying the specific list of values in every instance. It would be much easier to double-check my code that way and it would be more flexible.

Example Code

Here's a toy example:

Suppose I have a function simulates data that lies approximately on a parabolic curve. I would like to calculate datasets for three combinations of polynomial coefficients as well as the difference in the y-values for the arrays from a reference array.

x= @(NN) -NN:NN; %simulate x data from -NN -> NN

y = @(y0,a,b,NN) y0 + a*x(NN) + b*x(NN).^2 %simulate parabolic data

yREF = y(1,2,3,10); %generate the reference array

yDIFF = @(y0,a,b,NN) yREF - y(y0,a,b,NN) %

xDATA = x(10); %use common x values

yDATA1 = y(0.85,2.1,3.2,10); %dataset 1

yDATA2 = y(1.1,1.9,3.5,10); %dataset 2

yDATA3 = y(1.02,1.98,2.9,10); %dataset 3

To generate the differences I have to manually copy the parameter values which is annoying:

yDIFF1 = yDIFF(0.85,2.1,3.2,10); %differences for dataset 1

yDIFF2 = yDIFF(1.1,1.9,3.5,10); %differences for dataset 2

yDIFF3 = yDIFF(1.02,1.98,2.9,10); %differences for dataset 3

%Plot to visualize

figure;

subplot(2,1,1)

plot(xDATA,yDATA1,'.')

hold on

plot(xDATA,yDATA2,'.')

plot(xDATA,yDATA3,'.')

plot(xDATA,yREF,'--')

xlabel('x')

ylabel('y')

subplot(2,1,2)

plot(xDATA,yDIFF1,'.')

hold on

plot(xDATA,yDIFF2,'.')

plot(xDATA,yDIFF3,'.')

xlabel('x')

ylabel('y-y_{ref}')

Question

Is there a way where I can define the arguments just once and use them later? I'm imagining something like the following

coefs1 = [0.85 2.1 3.2 10]

coefs2 = [1.1 1.9 3.5 10] %etc

yDATA1 = y(coefs1)

yDIFF1 = y(coefs1) %etc

I'm sure this is trivial but I can't figure out how to do it. Thanks!!

##### 0 Comments

### Accepted Answer

Joseph Cheng
on 30 Jun 2021

Edited: Joseph Cheng
on 30 Jun 2021

i think you just answered yourself in your question

y = @(y0,a,b,NN) y0 + a*x(NN) + b*x(NN).^2; %simulate parabolic data

yDIFF = @(y0,a,b,NN) yREF - y(y0,a,b,NN); %

where instead of defining with @(y0,a,b,NN) you'd just define it with @(coefs) and then

y=@(coefs) coefs(1)+ coefs(2) * x(coefs(4)) + coefs(3) * x(coefs(4)).^2;

and if that wasn't clear, its because by making coefs# into the array = [y a b NN], by doing the above with coef(1~4) you're access y,a,b, and NN.

x= @(NN) -NN:NN; %simulate x data from -NN -> NN

y = @(y0,a,b,NN) y0 + a*x(NN) + b*x(NN).^2 ;%simulate parabolic data

yREF = y(1,2,3,10); %generate the reference array

yDIFF = @(y0,a,b,NN) yREF - y(y0,a,b,NN); %

xDATA = x(10); %use common x values

yDATA1 = y(0.85,2.1,3.2,10); %dataset 1

yDIFF1 = yDIFF(0.85,2.1,3.2,10); %differences for dataset 1

coefs1 = [0.85 2.1 3.2 10];

y2=@(coefs) coefs(1)+ coefs(2) * x(coefs(4)) + coefs(3) * x(coefs(4)).^2;

yDIFF2 = @(coefs) yREF - y2(coefs) ;%

yData2 = y2(coefs1);

yDiff2 = yDIFF2(coefs1);

[yDATA1;yData2]

[yDIFF1;yDiff2]

##### 2 Comments

Joseph Cheng
on 30 Jun 2021

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!