- yyaxis: https://www.mathworks.com/help/matlab/ref/yyaxis.html
- ylim: https://www.mathworks.com/help/matlab/ref/ylim.html
Plot two functions in one diagramm with different y-axis limits
4 views (last 30 days)
Show older comments
Does someone know how i could solve the following Problem:
%x_1,...,x_7 are real valued Variables (which are generated before)
g = 0.5*x_1*(x_2*f_1-x_3*f_2)^2+x_4*f_1+x_5*f_2+x_6-x_7;
funct = solve(g,f_2,'Real',true,'ReturnConditions',true);
y_lim = [10,15];
syms f_1
assume(funct.conditions);
funct.f_2(isAlways(funct.f_2 < y_lim(1),Unknown="falseWithWarning")) = NaN;
funct.f_2(isAlways(funct.f_2 > y_lim(2),Unknown="falseWithWarning")) = NaN;
fplot(funct.f_2, [7,50] ,'Color', 'b');
So i want to plot the quadratic function "funct" in the y-Intervall [10,15] and x-Intervall [7,50] but i get the (4) Warnings:
Warning: Unable to prove '(423*f_1)/77 - (576460752303423488000000*(7353528183219767092002251709184784741/324518553658426726783156020576256000000 -
(907661965900503303521461870041895139*f_1)/16615349947311448411297588253504307200000)^(1/2))/18611613717480648557 -
86464670358732992000000/18611613717480648557 < 10'.
> In symengine
In sym/isAlways (line 41)
So the function gets plottet and the x-Interval is respected, but not the y-Interval. If i set Unknown="true" my function is not plotted at all. Do you know how i could plot the function "funct" correctly (in both intervals)? Thanks
0 Comments
Answers (1)
Jaswanth
on 16 Oct 2024
Hi,
To plot two functions with different y-axis limits in MATLAB, you can use two y-axes on the same plot with the “yyaxis” function.
Assuming you are solving for (f_2) in terms of (f_1), use “fplot” to plot the functions over the specified x-interval.
Next, use the “yyaxis” function with “yyaxis left” and “yyaxis right” to create two y-axes, one for each function, switching between the left and right y-axes. Use “ylim” to set the y-axis limits for each axis.
Please refer to the following example to structure the code for plotting two functions with different y-axis limits:
% Define symbolic variables
syms f_1 f_2;
% Define your equation
x_1 = 1; x_2 = 2; x_3 = 3; x_4 = 4; x_5 = 5; x_6 = 6; x_7 = 7; % Example values
g = 0.5*x_1*(x_2*f_1-x_3*f_2)^2 + x_4*f_1 + x_5*f_2 + x_6 - x_7;
% Solve for f_2
funct = solve(g, f_2, 'Real', true, 'ReturnConditions', true);
% Assume conditions
assume(funct.conditions);
% Define y-axis limits
y_lim1 = [10, 15]; % Limits for first function
y_lim2 = [0, 20]; % Limits for second function (example)
% Create figure
figure;
% Plot first function
yyaxis left;
fplot(funct.f_2, [7, 50], 'Color', 'b');
ylim(y_lim1);
ylabel('f_2 Values');
% Plot second function (for demonstration, using a simple example function)
yyaxis right;
fplot(@(f_1) sin(f_1), [7, 50], 'Color', 'r');
ylim(y_lim2);
ylabel('sin(f_1)');
% Add labels and title
xlabel('f_1 Values');
title('Plot with Two Y-Axes');
% Add legend
legend('f_2', 'sin(f_1)');
Kindly refer to the following MathWorks documentation to know more about the functions discussed above:
I hope the solution provided above is helpful.
0 Comments
See Also
Categories
Find more on Calculus 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!