matlab程序:%cf对应的af不唯一,取af大于零的时候 ar=0:0.5:10; syms x assume(x>0) %根据魔术公式求导得到ar-cr的关系,求的cr,cf a0=1.5999;a1=-0.0048;a2=0.9328;a3=4.0847;a4=44.8338; a6=-0.0076;a7=-0.1807;a8=-0.0026;a9=0.0367; a11=0.0004;a12=-0.0115;a17=0.0009; F_zr=m*9.8*lf/(lf+lr)/1000; C=a0*(5-a)/4; D2=(a1*(F_zr^2)+a2*F_zr)*a; B2=(a3*sin(2*atan(F_zr/a4))/(C*D2))*(2-a); Sh2=a8*F_zr+a9; E2=(a6*F_zr+a7); cr=(1000*C*D2*cos(C*atan(E2*(atan(B2*ar) - B2*ar) + B2*ar)).*(B2 - E2*(B2 - B2./(B2^2*ar.^2 + 1))))./((E2*(atan(B2*ar) - B2*ar) + B2*ar).^2 + 1); cf=(m*V^2*lr*cr)./(cr*(lf+lr)*(lf+lr)-m*V^2*lf); % 已知参数 F_zf=m*9.8*(lr)/(lr+lf)/1000; D1=(a1*(F_zf^2)+a2*F_zf)*a; B1=(a3*sin(2*atan(F_zf/a4))/(C*D1))*(2-a); E1=a6*F_zf+a7; % 定义af-cf函数 f=@(x)(1000*C*D1*cos(C*atan(E1*(atan(B1*x) - B1*x) + B1*x)).*(B1 - E1*(B1 - B1./(B1^2*x.^2 + 1))))./((E1*(atan(B1*x) - B1*x) + B1*x).^2 + 1) - cf; % 反求af x0=[0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]+1; af=fsolve(f,x0); %转化为弧度制 af1=af*pi/180;ar1=ar*pi/180; %求得侧偏角和横摆角速度 r=(V*(cetia-af1+ar1))/(lf+lr); betia=(lf*(cetia-af1)-lf*ar1)/(lf+lr); figure(5); plot(betia,r); axis([-40,40,-40,40]); title('betia-r'); xlabel('betia');ylabel('r'); hold on;报错警告: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead. > 位置:fsolve (第 342 行) 位置: untitled2 (第 36 行) No solution found. fsolve stopped because the last step was ineffective. However, the vector of function values is not near zero, as measured by the value of the function tolerance. <stopping criteria details> >> 请修改
时间: 2023-12-16 13:03:12 浏览: 213
根据错误提示,可以看出问题出现在 fsolve 函数中,具体原因是“Trust-region-dogleg algorithm”算法不能处理非方阵系统。建议尝试使用其他算法,比如 Levenberg-Marquardt 算法。可以在 fsolve 函数中添加选项来指定算法类型,例如:
```
options = optimoptions('fsolve','Algorithm','levenberg-marquardt');
af=fsolve(f,x0,options);
```
另外,可以尝试调整函数 f 的初始值 x0,或者增加迭代次数。如果仍然无法解决问题,建议检查一下输入参数是否正确,或者是否存在其他错误。
相关问题
matlab用for循环用plot函数标注曲线y1=0.2e^-0.5x×cos(4πx)和y2=1.5e^-0.5x×cos(πx)的所有交点
可以使用MATLAB自带的solve函数来求解两条曲线的交点,具体代码如下:
syms x
y1 = 0.2*exp(-0.5*x)*cos(4*pi*x);
y2 = 1.5*exp(-0.5*x)*cos(pi*x);
sol = solve(y1 == y2);
x_intersect = double(sol)
y_intersect = 0.2*exp(-0.5*x_intersect)*cos(4*pi*x_intersect)
然后,可以使用for循环和plot函数来标注所有交点的位置,代码如下:
figure;
hold on;
fplot(y1, [0, 10]);
fplot(y2, [0, 10]);
for i = 1:length(x_intersect)
plot(x_intersect(i), y_intersect(i), 'ro');
text(x_intersect(i), y_intersect(i), ['(', num2str(x_intersect(i)), ', ', num2str(y_intersect(i)), ')']);
end
hold off;
当然,如果你只需要求解交点的位置,可以直接使用solve函数,不需要使用for循环和plot函数。
syms t; p = 2; f = matlabFunction(sqrt(1/(2*p))*exp(-0.5*t^2)); f_fun = @(x) integral(@(t) f(t), 0.5, x); g_fun = @(x) f_fun(x) - 0.45; w = [5/9, 8/9, 5/9]; x_nodes = [-sqrt(3/5), 0, sqrt(3/5)]; x0 = 0.5; tol = 1e-6; df_fun = matlabFunction(diff(f(t), t)); g1_fun = @(x) f_fun(x0) + w(1)*f_fun(x0 + 0.5*(x-x0)*x_nodes(1)) + w(2)*f_fun(x) + w(3)*f_fun(x0 + 0.5*(x-x0)*x_nodes(3)); g2_fun = @(x) x - g1_fun(x)/integral(@(t) f(t)/df_fun(t), 0, x); n = 0; xn = x0; while true n = n + 1; gx = g_fun(xn); gpx = diff(g_fun(t)); gpx = subs(gpx, t, xn); fprintf('迭代次数:%d, xn = %.6f, g(xn) = %.6f, g''(xn) = %.6f\n', n, xn, gx, gpx); xn1 = g2_fun(xn); if abs(xn1 - xn) < tol break; end xn = xn1; end fprintf('方程的根为:%.6f,迭代次数:%d\n', xn1, n); root = fzero(g_fun, x0); fprintf('使用fzero函数检验,方程的根为:%.6f\n', root);修改代码用matlabR2015bb
syms t; p = 2; f = matlabFunction(sqrt(1/(2*p))*exp(-0.5*t^2)); f_fun = @(x) integral(@(t) f(t), 0.5, x); g_fun = @(x) f_fun(x) - 0.45; w = [5/9, 8/9, 5/9]; x_nodes = [-sqrt(3/5), 0, sqrt(3/5)]; x0 = 0.5; tol = 1e-6; df_fun = matlabFunction(diff(f(t), t)); g1_fun = @(x) f_fun(x0) + w(1)*f_fun(x0 + 0.5*(x-x0)*x_nodes(1)) + w(2)*f_fun(x) + w(3)*f_fun(x0 + 0.5*(x-x0)*x_nodes(3)); g2_fun = @(x) x - g1_fun(x)/integral(@(t) f(t)/df_fun(t), 0, x); n = 0; xn = x0; while true n = n + 1; gx = g_fun(xn); gpx = diff(g_fun(t)); gpx = subs(gpx, t, xn); fprintf('迭代次数:%d, xn = %.6f, g(xn) = %.6f, g''(xn) = %.6f\n', n, xn, gx, gpx); xn1 = g2_fun(xn); if abs(xn1 - xn) < tol break; end xn = xn1; end fprintf('方程的根为:%.6f,迭代次数:%d\n', xn1, n); root = fzero(g_fun, x0); fprintf('使用fzero函数检验,方程的根为:%.6f\n', root);
阅读全文