Plot how the input changes
3 views (last 30 days)
Show older comments
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);
0 Comments
Answers (1)
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)
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
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.
.
See Also
Categories
Find more on Title 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!