Please help me. I want to run this attached simple code
Show older comments
%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
Accepted Answer
More Answers (1)
Matt J
on 5 Jul 2025
0 votes
Do what the error message says. Use .^ instead of ^
2 Comments
Tarek
on 5 Jul 2025
Edited: Walter Roberson
on 5 Jul 2025
What you used:
^
What Matt J and the error message advised you to use:
.^
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!