Hello, if i have two equations 1)2x+2 and 2)3x^2+ 4x-7 is there a way to remove the x in a new equation where i am left with 1)2+2 2)3^2+4-7? i would hope it would work with any equation with x in
Mostrar comentarios más antiguos
Hello, if i have two equations 1)2x+2 and 2)3x^2+ 4x-7 is there a way to remove the x in a new equation where i am left with 1)2+2 2)3^2+4-7? i would hope it would work with any equation with x in
2 comentarios
Ameer Hamza
el 24 de Abr. de 2020
Are these equations defined using symbolic variables?
Elliott Cameron
el 24 de Abr. de 2020
Respuestas (1)
Walter Roberson
el 24 de Abr. de 2020
syms x
eqn2 = 3*x^2 + 4*x - 7
subs(eqn2, x, 1)
Or numerically instead of symbolically:
eqn2 = @(x) 3*x.^2 + 4*x - 7;
eqn2(1)
14 comentarios
Elliott Cameron
el 24 de Abr. de 2020
Walter Roberson
el 24 de Abr. de 2020
Editada: Walter Roberson
el 25 de Abr. de 2020
x = 0:0.02:1;
y = 0.5*x + 2;
probe_location = 1;
S = interp1(x, y, probe_location);
This does not depend upon the probe location being exactly the same as one of the x values. In the exact form written, it does depend upon the probe location being within the bounds of x.
You might be tempted to use something like
x = 0:0.02:1;
y = 0.5*x + 2;
probe_location = 1;
xidx = find(x == probe_location);
S = y(xidx);
However, due to floating point round-off, most values you might try are not exactly available in x, because 0.01 is not exactly representable in binary floating point.
Elliott Cameron
el 24 de Abr. de 2020
Elliott Cameron
el 24 de Abr. de 2020
Walter Roberson
el 25 de Abr. de 2020
Could you confirm that for 3x^2+ 4x-7 and x = 1 that you would want 3^2+4-7 which would imply 9+4-7 implying 6, even though 3*1^2+4*1-7 would be 3+4-7 implying 0 ?
Will this all only ever need to work for x = 1, or will it need to work for finding other values as well? For example if the location it found was x = 0.5 then would you want the output to be 30.5^2+40.5-7 implying 963.75 or would you want the output to be 31/2^2+41/2-7 implying 21.25 ? Or would you want the output as 3*0.5^2+4*0.5-7 ? Because if so then why should the output for x = 1 be 3^2+4-7 instead of 3*1^2+4*1-7 ?
We need a list of the rules to be used in producing the output, including for x integer, x fractional, x negative, x complex valued -- or those possibilities need to be explicitly disallowed.
Elliott Cameron
el 25 de Abr. de 2020
Walter Roberson
el 25 de Abr. de 2020
syms x
eqn2 = 3*x^2 + 4*x - 7
eqn2show = regexprep(char(eqn2), 'x', '1');
In my test this gave '4*1 + 3*1^2 - 7', with the char() step changing the order for reasons unknown.
If it is not acceptable that the order might come out strange, then how easy it is to process will depend upon whether you can be sure that the equation is a polynomial.
Ameer Hamza
el 25 de Abr. de 2020
I am not sure what is the ultimate purpose of doing this, but it appears that OP does not care about it being a symbolic equation at all. In that case, it might be better to define the equation as char array directly
eqn1 = '3x^2 + 4x - 7';
eqn1_new = regexprep(eqn, 'x', '');
eqn2 = '2x+2';
eqn2_new = regexprep(eqn2, 'x', '');
eqn3 = '0.5x+2';
eqn3_new = regexprep(eqn3, 'x', '');
Result:
>> eqn1_new
eqn1_new =
'3^2 + 4 - 7'
>> eqn2_new
eqn2_new =
'2 + 2'
>> eqn3_new
eqn3_new =
'0.5 + 2'
Walter Roberson
el 25 de Abr. de 2020
I would hope that 3^2 is not really the desired output: that is too confusing for readers.
Ameer Hamza
el 25 de Abr. de 2020
Yes, that does not make much sense. I think that OP is a bit confused about the required output.
Elliott Cameron
el 26 de Abr. de 2020
Walter Roberson
el 26 de Abr. de 2020
disp(eqn1_new)
Elliott Cameron
el 26 de Abr. de 2020
Walter Roberson
el 26 de Abr. de 2020
The '' are not part of eqn1_new, they are decoration that MATLAB adds when you display a variable by just giving the name of the variable. disp() is not changing the value, it is just not adding the decoration as it displays the data.
Because of this, although you could
p = evalc('disp(eqn1_new)');
to in some sense capture the display without the '', then as soon as you went to display p just by mentioning its name, MATLAB would add the '' decoration to that -- and you would also have accidentally captured the newline that disp() added. You are no further ahead than if you had just done
p = eqn1_new;
Do not confuse the content of eqn1_new with the decoration that MATLAB adds when displaying it.
Categorías
Más información sobre Assumptions en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!