I'm trying to use the "solve" function to find a variable, but the other variables in the equation are input as opposed to defined. How do I fix this? Here's to illustrate what I'm trying to do using a simplified equation:
x=input('x = ')
y=input('y = ')
z=solve('x+y=z',z)

2 Kommentare

Tebikew Alemu
Tebikew Alemu am 27 Dez. 2021
How i can write a matlab cod for a funtion
Walter Roberson
Walter Roberson am 27 Dez. 2021
Bearbeitet: Walter Roberson am 27 Dez. 2021
Example:
syms x
y(x) = sin(x) - x^3/27 %this is the code for the function
y(x) = 
fplot(y, [-2*pi 2*pi])

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 9 Mär. 2011

0 Stimmen

Did you solve the license problem? Until you have the software installed and licensed, solve() isn't going to work.
The answer you are looking for is along these lines:
syms x y z
xin = input('x =');
yin = input('y =');
zsol = solve('z-y=x', z);
disp(['z = ' char(zsol)]);
znum = double(subs(zsol, {x, y}, {xin, yin}));
disp(['z = ' num2str(znum)]);

1 Kommentar

Patrick Star
Patrick Star am 9 Mär. 2011
I installed the software and this works. Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Andrew Newell
Andrew Newell am 7 Mär. 2011

0 Stimmen

Here is one way:
x=sym(input('x = '))
y=sym(input('y = '))
syms z
z=solve(z-x-y,z)

5 Kommentare

Patrick Star
Patrick Star am 8 Mär. 2011
I tried that, but it returns this error:
??? Undefined function or method 'sym' for input arguments of type 'double'.
Error in ==> test at 1
x=sym(input('x = '))
Walter Roberson
Walter Roberson am 8 Mär. 2011
Do you have the symbolic toolbox? What happens if you try
sym('x')
and
sym('1')
?
You do not really need the sym() calls for x and y:
x = input('x = ');
y = input('y = ');
syms z
z = solve(z-x-y,z);
Patrick Star
Patrick Star am 8 Mär. 2011
I don't think I have the symbolic toolbox because this is what it returns when I try sim('x'):
??? Undefined function or method 'sym' for input arguments of type 'char'.
Error in ==> test at 1
sym('x')
Andrew Newell
Andrew Newell am 8 Mär. 2011
Oh. I assumed that you were actually using the command "solve", which is in the Symbolic Toolbox.
Walter Roberson
Walter Roberson am 8 Mär. 2011
Please check to see whether you are licensed for the symbolic toolbox but perhaps have just not installed it. If you happen to be using the Student Edition than this could be the difficulty.
solve() is part of the symbolic toolbox and requires that toolbox be installed and licensed.

Melden Sie sich an, um zu kommentieren.

Andrew Newell
Andrew Newell am 8 Mär. 2011

0 Stimmen

Is this what you're after?
x=input('x = ')
y=input('y = ')
z = x+y

1 Kommentar

Patrick Star
Patrick Star am 8 Mär. 2011
In a way, yes. But the equations and variables are a lot more complicated than that and I don't have it already solved. I need to solve for z and find its value, but I can't figure out how to make the solve function give me both; in other words, I have an equation like z-y=x, and I want to input 1 and 2 for x and y and then have the output give me two things:
z = x+y
and
z = 3

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by