How to Code two Variable Feval??
Show older comments
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 Comments
Shubham Gupta
on 7 Nov 2019
Not sure what is 'x' in your input variables?
Then, inside the function doubsimpint, in 1st loop at line:
K1 = f(x,f(x,c)) + f(f(x,d));
has only 1 input for f(f(x,d)) , I think it should be f(x,f(x,d))?
Finally, inside the 2nd "for" loop at line:
y = feval(c,x)+j*HX; % for feval c should be a function, you meant to write f?
Don
on 7 Nov 2019
Shubham Gupta
on 7 Nov 2019
I am not talking about the 'x' inside the function but which you have used to define "input variables" i.e. 'c' & 'd'
Don
on 7 Nov 2019
Don
on 7 Nov 2019
Walter Roberson
on 7 Nov 2019
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.
Answers (0)
Categories
Find more on Common Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!