Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How to pass anonymous handle in a for loop?

1 vue (au cours des 30 derniers jours)
Roshni Khetan
Roshni Khetan le 5 Juin 2018
Clôturé : MATLAB Answer Bot le 20 Août 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)

Réponses (1)

Walter Roberson
Walter Roberson le 5 Juin 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

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by