Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

How to pass anonymous handle in a for loop?

1 visualización (últimos 30 días)
Roshni Khetan
Roshni Khetan el 5 de Jun. de 2018
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
function [o] = testScript(x)
input1 = [2,3,4,5,6,7,8];
input2 = [3,9,1.3,4,0.9,1.1,1.2];
output = [2.5071,11.5204,1.3981,6.0947,1.2437,1.7801,2.2177];
Re = input1.*(input2.^2);
Pr = input2./input1;
Var3 = input1+input2;
Var4 = input1.*input2;
for i = 1:7
syms x(i)
h(i) = @(x) (x(1)*(Re(i)^x(2))*(Pr(i)^x(3)));
k(i) = @(x) x(4)*(Re(i)^x(5))*(Var3(i)^x(6))*(Var4(i)^x(7));
o(i) = output(i) - h(x)(i) - k(x)(i);
end
end
I am trying to fsolve to calculate the value of x,but it gives me error - looks like I am not using the right command to pass handles to the function
as % fun = @testScript
% x0 = [ 0.2,0.3,0.4,0.5,0.15,0.2,0.3];
% x = fsolve(fun,x0)

Respuestas (1)

Walter Roberson
Walter Roberson el 5 de Jun. de 2018
You cannot store function handles indexed with () brackets.
for i = 1:7
syms x(i)
h{i} = @(x) (x(1)*(Re(i)^x(2))*(Pr(i)^x(3)));
k{i} = @(x) x(4)*(Re(i)^x(5))*(Var3(i)^x(6))*(Var4(i)^x(7));
o(i) = output(i) - h{i}(x) - k{i}(x);
end

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by