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

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

Respuestas (1)

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

if i already had x=0:0.02:1
y=0.5*x + 2
how would I find y when x =1? in a new variable called S
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.
i will try this out, thank you very much
the only problem is this gives me a number, i would like it to be kept at 0.5+2
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.
I only need it once for x=1 not for any other number so if there was a replace function where by i could replace x with 1 that would also work but i dont want it to be just one value. I would like it kept in an equation format
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.
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'
I would hope that 3^2 is not really the desired output: that is too confusing for readers.
Yes, that does not make much sense. I think that OP is a bit confused about the required output.
is there a way to do it without having it as text in '' ?
brillaint thank you so much, one last question if i wanted to make the disp(eqn1_new) into a new equation known as p?
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.

Iniciar sesión para comentar.

Productos

Etiquetas

Preguntada:

el 24 de Abr. de 2020

Comentada:

el 26 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by