我有一个功能
func(u1,u2,t)
现在我想在
时找到u2的值0 = func(u1, u2, t)
其中u1=a
和t=b
是已知值。我怎样才能在MATLAB中执行此操作(使用fsolve
或其他函数)?感谢
答案 0 :(得分:1)
这应该有用。
auxfunc = @(u2) func(a,u2,b)
x = fsolve(auxfunc,x0)
您可以使用anonymous function expression从任何功能创建辅助功能。对于这种情况,您将函数func
的第一个和第三个参数替换为已知值(a
和b
)以创建仅使用一个参数的新函数auxfunc
(u2
)。然后将生成的辅助函数放到fsolve
,并进行一些初始猜测x0
。
答案 1 :(得分:1)
这是一个简单的例子。
function main // create a main function so that you can call `func`. There is another way to do it, but it doesn't matter here.
u2_0 = 0.25; // provide an initial guess for the solution you are seeking
options = optimset('Display', 'iter'); // ask the solver to output information on the command window
[u2_opt, fval] = fsolve(@func, u2_0, options); // solve func(u2) = 0
x = -2*pi:0.1:2*pi; // plot func
plot(x, func(x), 'LineWidth', 2)
hold on
grid on
plot(u2_0, func(u2_0), 'g.', 'MarkerSize', 16) // plot the initial guess on the graph of func (green point)
plot(u2_opt, fval, 'r.', 'MarkerSize', 16) // plot the result of the solver on the graph of func (red point)
axis([-8 8 -1.25 1.25])
function y = func(u2) // define func
u1 = 1.0;
t = 2/pi;
y = u1*cos(t*u2);
end
end
示意性地
您阅读有关警告,可用算法,参数调整等的文档越多越好。没有一个具有完全一般范围且可以盲目信任的求解器。
希望有所帮助。
答案 2 :(得分:0)
如果它只是一个(标量)变量,那么使用 fzero 比 fsolve 更好。