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

4 Ansichten (letzte 30 Tage)
Tarek
Tarek am 5 Jul. 2025
Kommentiert: Tarek am 6 Jul. 2025
%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

Akzeptierte Antwort

Torsten
Torsten am 5 Jul. 2025
Bearbeitet: Torsten am 5 Jul. 2025
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 Kommentare
Torsten
Torsten am 6 Jul. 2025
Bearbeitet: Torsten am 6 Jul. 2025
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.
Tarek
Tarek am 6 Jul. 2025

Thanks alot Professor Torsten

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 5 Jul. 2025
Do what the error message says. Use .^ instead of ^
  2 Kommentare
Tarek
Tarek am 5 Jul. 2025
Bearbeitet: Walter Roberson am 5 Jul. 2025
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);
%
Stephen23
Stephen23 am 5 Jul. 2025
Bearbeitet: Stephen23 am 5 Jul. 2025
What you used:
^
What Matt J and the error message advised you to use:
.^

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by