Plot how the input changes

3 views (last 30 days)
William Lancelot
William Lancelot on 14 Oct 2021
Commented: Star Strider on 14 Oct 2021
I am trying to plot how a variable changes in a function with two variables and an output.
For example, consider the function y = m*x+b, I will supply one value of y, a fixed value for x, and multiple values (linspace) for m, and I want to plot how b (y-axis) changes with the values of m. Later, I will change y to another value and I want to be able to repeat the process. Is this possible? I am fairly new to MATLAB and wrote the following incorrrect lines:
x = 1;
m = linspace(0,10,11);
y=2;
syms y m b
eqn = m*x+b == y;
soln = solve(eqn,b);
fplot(m,soln);

Answers (1)

Star Strider
Star Strider on 14 Oct 2021
Try something like this —
b = @(m,x,y) y-m.*x;
x = 1;
m = linspace(0,10,11);
y=2;
figure
plot(m, b(m,x,y))
grid
xlabel('m')
ylabel('b')
x = 1:11;
m = linspace(0,10,11);
y = 2:12;
[xm,ym] = ndgrid(x,y);
Q1 = b(m,xm,ym)
Q1 = 11×11
2 2 2 2 2 2 2 2 2 2 2 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 2 0 -2 -4 -6 -8 -10 -12 -14 -16 -18 2 -1 -4 -7 -10 -13 -16 -19 -22 -25 -28 2 -2 -6 -10 -14 -18 -22 -26 -30 -34 -38 2 -3 -8 -13 -18 -23 -28 -33 -38 -43 -48 2 -4 -10 -16 -22 -28 -34 -40 -46 -52 -58 2 -5 -12 -19 -26 -33 -40 -47 -54 -61 -68 2 -6 -14 -22 -30 -38 -46 -54 -62 -70 -78 2 -7 -16 -25 -34 -43 -52 -61 -70 -79 -88
figure
plot(m, b(m,xm,ym))
grid
xlabel('m')
ylabel('b')
figure
meshc(m, y, b(m,xm,ym))
grid on
xlabel('m')
ylabel('y')
zlabel('b')
view(60,30)
Expoeriment to get different results.
.
  2 Comments
William Lancelot
William Lancelot on 14 Oct 2021
Hello - thank you for your help and reply. The problem is that I don't want to change the original function and solve for the desired variable; y = mx+b is just a reproducible example to simplify what I am trying to reach, but the one I am working on isn't easily solvable
Star Strider
Star Strider on 14 Oct 2021
My pleasure!
It depends on what the function is. Here it’s a symbolic function, however if it’s numeric and can be cast as a numeric function (similar to what I did here), it can possibly be solved using fsolve, likely looping over the various variables and solving of a specific other variable. In that situation, ‘xm’ and ‘ym’ (and perhaps others) would be column vectors, each addressed in a nested loop. The result would be an array of some description.
This can of course get very complicated very quickly, specifically if it’s an equation with possibly multiple roots, in which case solving for all of them numerically could be difficult.
.

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!