How to graph alternative solution of a function
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
HI there, pretty simple question and more of a maths one than a technical MATLAB one.
For each value of Launch Angle there are two solutions for the value of A. I'm plotting one at the moment and want to plot the other. e.g. at launch angle of 10deg, A can equal both 0.21 and 2.2ish
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/285414/image.png)
How do I change my code to plot the other solution?
A_values = ones(1,45);
B_values = ones(1,45);
LaunchAngle = ones(1, 45);
for qL = 1:45
LaunchAngle(1,qL) = qL;
qLrad = deg2rad(qL);
Rd = 1.7; % Y asymptote of curve
XL = 0; % Release point x
X0 = 1.7; % Starting point x
F = @(A,B) [((-Rd + ((2*Rd) /(1 + exp(A*(X0 - B)))))), (((-2*A*Rd*exp(A*(XL-B)))/((exp(A*(XL-B))+1)^2))-tan(qLrad))];
AB0 = [1; 1]*1j;
AB = fsolve(@(b)F(b(1),b(2)), AB0);
A_values(:,qL) = AB(1)*-1;
B_values(:,qL) = AB(2);
end
plot(LaunchAngle, A_values, LaunchAngle, B_values);
xlabel('Launch angle (deg)');
ylabel('Required trajectory function parameters');
title('Required trajectory function parameters for launch point x = 0');
legend('A', 'B');
0 Kommentare
Antworten (1)
Deepak
am 12 Nov. 2024
We can plot both possible solutions of (A) against the launch angles by using two distinct initial guesses for the “fsolve” function. By setting different initial conditions, such as AB0_1 and AB0_2, we guide “fsolve” to explore separate regions of the solution space, thereby identifying both solutions.
Next, we can store these solutions in separate arrays and plots them against the launch angles, allowing for a clear visualization of how each solution for (A) varies with the launch angle. This method ensures that both trajectories are represented, providing a comprehensive understanding of the behaviour of model.
Here is the MATLAB code to accomplish the same:
A_values1 = ones(1,45);
B_values1 = ones(1,45);
A_values2 = ones(1,45);
B_values2 = ones(1,45);
LaunchAngle = ones(1, 45);
for qL = 1:45
LaunchAngle(1,qL) = qL;
qLrad = deg2rad(qL);
Rd = 1.7; % Y asymptote of curve
XL = 0; % Release point x
X0 = 1.7; % Starting point x
F = @(A,B) [((-Rd + ((2*Rd) /(1 + exp(A*(X0 - B)))))), (((-2*A*Rd*exp(A*(XL-B)))/((exp(A*(XL-B))+1)^2))-tan(qLrad))];
% First solution
AB0_1 = [1; 1]*1j;
AB1 = fsolve(@(b)F(b(1),b(2)), AB0_1);
A_values1(:,qL) = AB1(1)*-1;
B_values1(:,qL) = AB1(2);
% Second solution
AB0_2 = [-2; -2]*1j;
AB2 = fsolve(@(b)F(b(1),b(2)), AB0_2);
A_values2(:,qL) = AB2(1)*-1;
B_values2(:,qL) = AB2(2);
end
figure;
plot(LaunchAngle, A_values1, 'b', LaunchAngle, A_values2, 'r');
xlabel('Launch angle (deg)');
ylabel('A values');
title('Two solutions for A at each launch angle');
legend('Solution 1', 'Solution 2');
figure;
plot(LaunchAngle, B_values1, 'b', LaunchAngle, B_values2, 'r');
xlabel('Launch angle (deg)');
ylabel('B values');
title('Two solutions for B at each launch angle');
legend('Solution 1', 'Solution 2');
Please find attached the documentation of functions used for reference:
I hope this helps in resolving the issue.
0 Kommentare
Siehe auch
Kategorien
Mehr zu MATLAB Mobile Fundamentals finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!