Please help me. I want to run this attached simple code

2 次查看(过去 30 天)
%Error using ^
%Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers,
%use '.^'.
%Error in proj/projfun (line 42)
% dy(2) = (4*b1*m^2+4*m^2*(b2-lamda).*p+4*b3*m^2*(p).^2+4*b4*m^2*(p).^3+4*b5*m^2.*p^4+a*(2*m+1)*(dp).^2)/(-2*a*m(2*m+1)*p);
%
%code%
function sol= proj
clc;clf;clear;
myLegend1 = {};myLegend2 = {};
rr = [1 2 4]
for i =1:numel(rr)
b1 = rr(i)
b2=0.5;b3=0.5;b4=0.5;
lamda=0.5;
y0 = [1,0,1,0,0,1,0,1];options =bvpset('stats','on','RelTol',1e-5);
m = linspace(-20,20);
solinit = bvpinit(m,y0);
sol= bvp4c(@projfun,@projbc,solinit,options);
figure(1)
plot(sol.x,(sol.y(1,:))^0.5)
% axis([0 4 0 1])
grid on,hold on
myLegend1{i}=['n= ',num2str(rr(i))];
figure(2)
plot(sol.x,(sol.y(2,:)))
%axis([0 4 -0.8 0])
grid on,hold on
myLegend2{i}=['n = ',num2str(rr(i))];
i=i+1;
end
figure(1)
legend(myLegend1)
hold on
figure(2)
legend(myLegend2)
function dy= projfun(~,y)
dy= zeros(8,1);
% alignComments
p = y(1);
dp = y(2);
dy(1) = dp;
dy(2) = (4*b1*m^2+4*m^2*(b2-lamda).*p+4*b3*m^2*(p).^2+4*b4*m^2*(p).^3+4*b5*m^2.*p^4+a*(2*m+1)*(dp).^2)/(-2*a*m(2*m+1)*p);
end
end
function res= projbc(ya,yb)
res= [ya(1);
ya(2);
yb(1);
yb(2);
% yb(7);
];
end

采纳的回答

Torsten
Torsten 2025-7-5
编辑:Torsten 2025-7-5
Use
function dy= projfun(x,y)
and replace m by x in the function.
Further, b5 and a are undefined.
Further, you miss a multiplication sign in this expression: (-2*a*m(2*m+1)*p) (in which you should already have replaced m by x).
Further, if you define a system of 2 differential equations, dy has to be of size 2, not 8. Thus you have to modify
y0 = [1,0,1,0,0,1,0,1]; and dy= zeros(8,1);
Same for the boundary conditions: you need 2, not 4.

更多回答(1 个)

Matt J
Matt J 2025-7-5
Do what the error message says. Use .^ instead of ^
  2 个评论
Tarek
Tarek 2025-7-5
编辑:Walter Roberson 2025-7-5
Hellow 👋 Mr Matt J.
Please help me.
The error is:
Error using ^
%Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers,
%use '.^'.
%Error in proj/projfun (line 42)
% dy(2) = (4*b1*m^2+4*m^2*(b2-lamda).*p+4*b3*m^2*(p).^2+4*b4*m^2*(p).^3+4*b5*m^2.*p^4+a*(2*m+1)*(dp).^2)/(-2*a*m(2*m+1)*p);
%

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by