Entering values into a function handle output

조회 수: 1 (최근 30일)
Rick
Rick 2014년 8월 13일
답변: Geoff Hayes 2014년 8월 13일
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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by