How do I pass coefficients to function handle as an array or list?
Mostrar comentarios más antiguos
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!!
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Assumptions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!