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

%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일

0 개 추천

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.

댓글 수: 3

Thank you Mr Torsten

Torsten
Torsten 2025년 7월 6일
편집: Torsten 2025년 7월 6일
You will get trouble with your dy(2) expression because you divide by (-2*a*m(2*m+1)*p) which is 0 for m = 0 and m = -1/2.

Thanks alot Professor Torsten

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2025년 7월 5일

0 개 추천

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);
%
What you used:
^
What Matt J and the error message advised you to use:
.^

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2025년 7월 5일

댓글:

2025년 7월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by