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

15 views (last 30 days)
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!!

Accepted Answer

Joseph Cheng
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]
ans = 2×21
299.8500 241.1500 188.8500 142.9500 103.4500 70.3500 43.6500 23.3500 9.4500 1.9500 0.8500 6.1500 17.8500 35.9500 60.4500 91.3500 128.6500 172.3500 222.4500 278.9500 341.8500 299.8500 241.1500 188.8500 142.9500 103.4500 70.3500 43.6500 23.3500 9.4500 1.9500 0.8500 6.1500 17.8500 35.9500 60.4500 91.3500 128.6500 172.3500 222.4500 278.9500 341.8500
[yDIFF1;yDiff2]
ans = 2×21
-18.8500 -15.1500 -11.8500 -8.9500 -6.4500 -4.3500 -2.6500 -1.3500 -0.4500 0.0500 0.1500 -0.1500 -0.8500 -1.9500 -3.4500 -5.3500 -7.6500 -10.3500 -13.4500 -16.9500 -20.8500 -18.8500 -15.1500 -11.8500 -8.9500 -6.4500 -4.3500 -2.6500 -1.3500 -0.4500 0.0500 0.1500 -0.1500 -0.8500 -1.9500 -3.4500 -5.3500 -7.6500 -10.3500 -13.4500 -16.9500 -20.8500
  2 Comments
Travis Briles
Travis Briles on 30 Jun 2021
I knew it would be trivial :). It should have occurred to me to try @(coefs). Thanks!
Joseph Cheng
Joseph Cheng on 30 Jun 2021
Talking from experience, you should definately either change the name coefs to something with y0abNN and/or put in a comment detailing how coeffs is poulated by y0, a b and NN.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!