Entering values into a function handle output

2 次查看(过去 30 天)
Hello, I have a function, and I can calculate P from the command window, but when I use this function, my output yh has P(1), P(2), etc, but what I want it do to is replace those with the actual value calculated
function yh = BreakRod(cellData)
Y = cellData{1,:}';
U = cellData{2,:}';
V = cellData{3,:}';
A = Adef(U,V);
P = A\Y;
yh = @(U,V) P(1) + P(2)*exp(U.^2) + P(3)*sqrt(V) + P(4)*U.*V;
end
function A = Adef(U,V)
N = size(U,1);
A = ones(N,4);
A(:,2) = exp(U.^2);
A(:,3) = sqrt(V);
A(:,4) = U.*V;
end
cellData{:}
ans =
1 4 5 6
ans =
1.0000 2.0000 3.0000 0.5000
ans =
2 1 7 3
P
P =
30.4842
0.0299
-3.4979
-12.3093
yh = BreakRod(cellData)
yh =
@(U,V)P(1)+P(2)*exp(U.^2)+P(3)*sqrt(V)+P(4)*U.*V

采纳的回答

Geoff Hayes
Geoff Hayes 2014-8-13
Rick - you can use the str2func function to convert a string to a function. Just build the function string by concatenating parts of the equation and use num2str to convert the P numeric data to strings
yh = str2func(['@(U,V) ' num2str(P(1)) ' + ' num2str(P(2)) ...
'*exp(U.^2) + ' num2str(P(3)) '*sqrt(V) + ' ...
num2str(P(4)) '*U.*V']);
And then
yh = BreakRod(cellData)
yh =
@(U,V)30.4842+0.029898*exp(U.^2)+-3.4979*sqrt(V)+-12.3093*U.*V
Try the above and see what happens!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by