How to Code two Variable Feval??
4 ビュー (過去 30 日間)
古いコメントを表示
I want to code a function that has two varibles in it but one of those variables a function.
Mainly HX = ....
and K1...
I attempted feval but it doesn't work so it put it back into typical math format
Inputs:
f = @(x,y) (x^2)+(y^2);
a = 2;
b = 2.2;
c = x;
d = 2*x;
n = 4;
m = 4;
function [di, i] = doubsimpint(f,a,b,c,d,m,n)
syms x y
h = (b - a)/n;
J1 = 0;
J2 = 0;
J3 = 0;
for i = 1:n
x = a + i*h;
HX = (feval(d,x) - feval(c,x))/m; %d(x) c(x)
K1 = feval(f,x,feval(c,x)) + feval(f,x,feval(d,x)); %f(x,c(x)) + f(x,d(x))
K2 = 0;
K3 = 0;
for j = 1:(m-1)
y = feval(c,x)+j*HX;
Q = feval(f,x,y);
if rem(j, 2) == 0 %if j is even
K2 = K2 + Q;
else
K3 = K3 + Q;
end
end
L = (K1 +2*K2 + 4*K3)*HX/3;
if i == 0 || i == n
J1 = J1 + L;
elseif rem(i, 2) == 0
J2 = J2 + L;
else
J3 = J3 + L;
end
end
J = h*(J1 + 2*J2 + 4*J3)/3;
di = J;
end
6 件のコメント
Walter Roberson
2019 年 11 月 7 日
Q = feval(f,x,y);
is valid syntax for feval a function that takes at least two parameters.
feval() is not used much now; it is only suitable for the case where your f might be a character vector or string object that gives the name of a function. In pretty much all modern cases you should be passing around function handles instead of names of functions.
回答 (0 件)
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!