array of anonymous function

hello,
I need help on anonymous function.
i need to calc array of obj [obj1, obj2, obj3] in loop each time , i call to obj with syms V and parameters value I0, IL, Rs, Rp, Vt_Ta which define before.
i try to define clussdef of obj, but get an errore.
Thanks a lot
for k=1:3
obj = @(V) I_fun(V, I0, IL, Rs, Rp, Vt_Ta) - target_value;
fplot(obj{k},[0 55],'LineWidth',2);
end

 Respuesta aceptada

Matt J
Matt J el 25 de Jun. de 2023
Editada: Matt J el 25 de Jun. de 2023
clear obj
for k=3:-1:1
obj{k} = @(V) I_fun(V, I0, IL, Rs, Rp, Vt_Ta) - target_value;
fplot(obj{k},[0 55],'LineWidth',2);
end

5 comentarios

david
david el 25 de Jun. de 2023
thanks about responde; i get this errore
"Unable to perform assignment because brace indexing is not supported for variables of this type."
for k=1:3
obj{K} = @(V) I_fun(V, I0(k), IL(k), Rs(k), Rp(k), Vt_Ta(k)) - target_value;
fplot(obj{k},[0 55],'LineWidth',2);
end
when
I0=[1.3e-10 1e-10 1e-10];
IL=[4.44 4.4 4.3];
Ns=72;
Rs=[3.6 3 2.7];
Rp=[120 130 190];
N=[0.95 0.96 0.97]
To what did you initialize the obj array prior to your loop? If you assigned an anonymous function to obj then tried to extend that array using brace indexing, that won't work. Preallocate the variable as a cell array and fill in the cells.
obj = cell(1, 5);
for k = 1:5
obj{k} = @(x) x.^k;
end
check = obj{4}(3) % 3^4 = 81
check = 81
Compare this with:
obj2 = @(x) x.^1;
for k = 2:5
obj2{k} = @(x) x.^k; % will error
end
Unable to perform assignment because brace indexing is not supported for variables of this type.
david
david el 25 de Jun. de 2023
Thanks you,
i try it but get an errore again, here the code i try to run
V = linspace (0,50);
I0=[1.3e-10 1e-10 1e-10];
IL=[4.44 4.4 4.3];
Ns=72;
Rs=[3.6 3 2.7];
Rp=[120 130 190];
N=[0.95 0.96 0.97]
Vt_Ta = [1.7885 1.7699 1.788];
obj = cell(1,3);
for k=1:3
obj{K} = @(V) I_fun(V, I0(k), IL(k), Rs(k), Rp(k), Vt_Ta(k)) - target_value;
fplot(obj{k},[0 55],'LineWidth',2);
end
V = linspace (0,50);
I(j) = zeros(size(V));
for j=1:6;
% Newton Raphson Method
I = I- (IL- I- I0.*( exp((V+I.*Rs)./Vt_Ta) -1)-(V+I.*Rs)/Rp)./ (-1 - I0.*(Rs/Vt_Ta)*exp((V+I.*Rs)./Vt_Ta)-Rs/Rp);
end
Paul
Paul el 25 de Jun. de 2023
did the error arise because the for loop index variable is k (lower case) , but the index variable into the obj cell array is K (upper case)?
david
david el 26 de Jun. de 2023
Thank, i see it later.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Performance and Memory en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Preguntada:

el 25 de Jun. de 2023

Comentada:

el 26 de Jun. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by