Search code examples
matlabmatlab-figuresymbolic-math

Why is this error showing up and could I plot a graph this way?


I am trying to plot a graph using the following code in MATLAB

pressure = linspace(10,2000,1000);
eqn = ((nh^2)/(1-nh))*(pressure*0.0680459639) == 0.136;
y1 = max(vpa(solve(eqn, nh)));
figure(2)
scatter(presssure, y1)
hold on
plot(pressure, 1-y1)
hold off

But this error showed up: "Input arguments must be convertible to floating-point numbers." for

y1 = max(vpa(solve(eqn, nh)));

Could you help me figure out what is wrong with my code and whether I can plot a graph this way? Thank you!

Edit: After I split it into "solve":

pressure = linspace(10,2000,1000);
eqn = ((nh^2)/(1-nh))*(pressure*0.0680459639) == 0.136;
y1 = solve(eqn, nh);

Warning: Solutions are parameterized by the symbols: z. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.

In sym/solve>warnIfParams (line 475) In sym/solve (line 357) In ae433hw5 (line 31) Warning: Solutions are only valid under certain conditions. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'. In sym/solve>warnIfParams (line 478) In sym/solve (line 357) In ae433hw5 (line 31)


Solution

  • I believe the issue here is that you are attempting to use an array within the equation before solving it causing MATLAB to break down into tears. What I have done is converted "nh" into a symbolic variable, created another symbolic variable "p" to represent pressure then solved the equation for nh before substituting the "p" symbolic for your "pressure" array and applying the vpa and max functions after. This provides me with a graph and a set of solutions for your equation.

    syms nh p
    pressure = linspace(10,2000,1000);
    eqn = ((nh^2)/(1-nh))*(p*0.0680459639) == 0.136;
    temp = solve(eqn, nh); % temporary equation placeholder
    y1 = max(vpa(subs(temp, p, pressure)));
    figure(2)
    scatter(pressure, y1)
    hold on
    plot(pressure, 1-y1)
    hold off