Kindly help me out with this code. its urgent please .thanks
5 views (last 30 days)
Show older comments
OGUNGBEMI Ezekiel
on 21 Jun 2023
Commented: Walter Roberson
on 21 Jun 2023
c=5.916;
h=35;
a=-11.5;
b=180;
q1=0;
q2=0;
p1=0.2;
E=0.2;
p2=sqrt(2*E- p1^2)
K=[0 0.0189 0.0190 2.2];
m=length(K);
dt=1/5;
tspan=[100:dt:1000];
for c=1:m;
[t{c},x{c}]=ode23tb(@(t,x)HB(t,x,c,h,a,b,K(c)),tspan,[q1 q2 p1 p2]);
end
X1=x{:,1};
X2=x{:,2};
X3=x{:,3};
X4=x{:,4};
subplot(2,2,1)
plot(X1(:,1),X1(:,3),'b',X1(:,2),X1(:,4),'r','LineWidth',5.0)
ax = gca;
ax.FontSize = 20; % Font Size of 15
legend('P','q')
title('(a)')
ylabel("P")
xlabel("q")
subplot(2,2,2)
plot(X2(:,1),X2(:,3),'b',X2(:,2),X2(:,4),'r','LineWidth',1.2)
ax = gca;
ax.FontSize = 20; % Font Size of 15
title('(b)')
ylabel("P")
xlabel("q")
subplot(2,2,3)
plot(X3(:,1),X3(:,3),'b',X3(:,2),X3(:,4),'r','LineWidth',1.2)
ax = gca;
ax.FontSize = 20; % Font Size of 15
title('(c)')
ylabel("p")
xlabel("q")
subplot(2,2,4)
plot(X4(:,1),X4(:,3),'b',X4(:,2),X4(:,4),'r','LineWidth',1.5)
ax = gca;
ax.FontSize = 20; % Font Size of 15
title('(d)')
ylabel("P")
xlabel("q")
function dxdt = HB(t,x,b,c,a,h,K)
dxdt = zeros(4,1);
dxdt(1) = x(3);
dxdt(2) = x(4);
dxdt(3) =-((((2*h.*sin.*x(1))./(cos^3.*(x(1))))+((c.*(cos^2)*x(1)+2.*(sin^2).*x(1))./((cos^3).*x(1)))-(b.*sin*2.*x(1))+(a.*cos.*x(1)))+ K*(x(2)-x(1)));
dxdt(4) =-((((2*h.*sin.*x(2))./(cos^3.*(x(2))))+((c.*(cos^2)*x(2)+2.*(sin^2).*x(2))./((cos^3).*x(2)))-(b.*sin*2.*x(2))+(a.*cos.*x(2)))+ K*(x(1)-x(2)));
end
2 Comments
Steven Lord
on 21 Jun 2023
What help are you looking to receive? You haven't asked a question or indicated the nature of the problem you're experiencing with this code.
Walter Roberson
on 21 Jun 2023
Is it "Send an ambulance!" urgent, or is it "Illegally pull over to the side of the side of the express highway to answer" urgent, or is it "Cancel my meeting with my cardiac surgeon!" urgent?
Accepted Answer
Steven Lord
on 21 Jun 2023
Looking at one sample line from your HB function:
dxdt(3) =-(((2*h*sin*x(1))/((cos)^3*(x(1))))+((c*(cos^2)*x(1)+2*(sin)^2*x(1))/((cos)^3*x(1)))-(b*(sin)*2*x(1))+(a*(cos)*x(1)))+ K*(x(2)-x(1));
None of sin*x(1), (cos)^3*(x(1)), (cos^2)*x(1), etc. will work.
To compute the sine of an angle call the sin function with an input. The same holds for cosine and the cos function.
sin(pi/2)
To compute a power of the sine or cosine of an angle call the sin or cos functions and raise the result to a power.
sin(pi/3)^2 % sine squared of pi/3
Yes, I know mathematically that operation is usually written
but that's not how it's written in code. Think
instead.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1416564/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1416569/image.png)
0 Comments
More Answers (1)
Torsten
on 21 Jun 2023
Edited: Torsten
on 21 Jun 2023
The trigonometric functions sin, cos for an argument x are invoked as sin(x), cos(x),...
If they have an exponent, they are either sin(x^3), cos(x^3),... or (sin(x))^3, (cos(x))^3 depending on what you want to do.
sin.*x(1) cos^3.*(x(1)) etc. like in your code make no sense.
3 Comments
Torsten
on 21 Jun 2023
Didn't you read what I wrote ? You didn't change anything in your sin and cos expressions in function HB.
Walter Roberson
on 21 Jun 2023
sin*x(1) asks matlab to invoke the sin function with no parameters, and multiply the results by x(1). However, matlab does not permit invoking sin with no parameters. If the intent is to invoke sin with an empty parameter use sin([])*x(1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!