transcedental equations in MATLAB [duplicate] - matlab

This question already has answers here:
solving nonlinear equations in Octave
(2 answers)
Closed 9 years ago.
How to solve a equation like 3^x + 4^x = 6^x in MATLAB . I want the solution exact to eight decimal digits .
I tried a very simplistic way but there is not enough memory for that . Since I know the solution is between 1 and 2 , I thought of creating an array x = [1:10^-9:2] and then use these arrays to find the value of correct x . I know this is very naive method .
How does one go about solving such equations in MATLAB ?

Use fzero:
>> f = #(x) 3^x + 4^x - 6^x
f =
#(x)3^x+4^x-6^x
>> x0 = [1 2]
x0 =
1 2
>> format long g
>> fzero(f,x0)
ans =
1.293174075673

Related

How to evaluate a matlab symbolic expression properly? [duplicate]

This question already has an answer here:
Evaluate symbolic expression
(1 answer)
Closed 4 years ago.
Lets say I have a matrix like this:
syms p;
K = [p^2+3 0; 2 5*p];
p_initial = 2;
Whats the proper/fastest way of getting K(p_initial), that is the resulting matrix if I insert 2 for p. Further, I want the resulting matrix to be of type double, not of symbolic type.
Thanks in advance
Use subs to substitute variables in symbolic expressions
subs(K,'p',p_initial)
ans =
[ 7, 0]
[ 2, 10]

Finding value that is similar and available in another vector [duplicate]

This question already has an answer here:
Matlab, finding common values in two array
(1 answer)
Closed 5 years ago.
Let say we have 2 vectors of A and B,
A=[1;2;5;6;7;9]; B=[1;3;4;7];
How to find value C that are available in both A and B? The expected value should be
C=[1;7]
Since the title of your question says "similar", I assume you want to compare with a given tolerance. For that you can use ismembertol:
tol = 1e-3;
A = [1; 2 ; 5 ; 6 ; 7 ; 9];
B = [1.0001; 3.0001; 4.0001; 7.0001];
ind = ismembertol(A, B, tol);
C = A(ind);
Very simple:
A=[1;2;5;6;7;9];
B=[1;3;4;7];
C=intersect(A,B)

How to round symbolic expressions to N digits in Matlab [duplicate]

This question already has an answer here:
Rounding to n significant digits
(1 answer)
Closed 5 years ago.
When you try rounding a symbolic expression to N digits you get the error messages below:
>> format long
>> syms x;
>> round(x, 10)
Error using sym/round
Too many input arguments.
>> round(vpa(pi), 10)
Error using sym/round
Too many input arguments.
So how do you make this work ?
Here it is how you do
>> syms x; N = 6;
>> round(pi*10^N)/vpa(10^N)
ans =
3.141593
>> round(x*10^N)/vpa(10^N)
ans =
0.000001*round(1000000*x)

How can I make reverse function of diff of Matlab? [duplicate]

I am trying to resolve why the following Matlab syntax does not work.
I have an array
A = [2 3 4 5 8 9...]
I wish to create an indexed cumulative, for example
s(1) = 2; s(2)=5, s(3)=9; ... and so on
Can someone please explain why the following does not work
x = 1:10
s(x) = sum(A(1:x))
The logic is that if a vector is created for s using x, why would not the sum function behave the same way? The above returns just the first element (2) for all x.
For calculating the cumulative sum, you should be using cumsum:
>> A = [2 3 4 5 8 9]
A =
2 3 4 5 8 9
>> cumsum(A)
ans =
2 5 9 14 22 31
The issue is that 1:x is 1 and that sum reduces linear arrays. To do this properly, you need a 2d array and then sum the rows:
s(x)=sum(triu(repmat(A,[prod(size(A)) 1])'))
You are asking two questions, really. One is - how do I compute the cumulative sum. #SouldEc's answer already shows how the cumsum function does that. Your other question is
Can someone please explain why the following does not work
x = 1:10
s(x) = sum(A(1:x))
It is reasonable - you think that the vector expansion should turn
1:x
into
1:1
1:2
1:3
1:4
etc. But in fact the arguments on either side of the colon operator must be scalars - they cannot be vectors themselves. I'm surprised that you say Matlab isn't throwing an error with your two lines of code - I would have expected that it would (I just tested this on Freemat, and it complained...)
So the more interesting question is - how would you create those vectors (if you didn't know about / want to use cumsum)?
Here, we could use arrayfun. It evaluates a function with an array as input element-by-element; this can be useful for a situation like this. So if we write
x = 1:10;
s = arrayfun(#(n)sum(A(1:n)), x);
This will loop over all values of x, substitute them into the function sum(A(1:n)), and voila - your problem is solved.
But really - the right answer is "use cumsum()"...
Actually what you are doing is
s(1:10)= sum(A(1:[1,2,3...10]))
what you should do is
for i=1:10
s(i)=sum(A(1:i))
end
hope it will help you

Interleaved repmat [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Element-wise array replication in Matlab
I have a m x 1 vector that I would like to repeat n times to create a (m*n)x1 vector. If I use repmat, I get something like
>> V = [a;b;c];
>> repmat(V,2,1) % n = 2, m = 3
a
b
c
a
b
c
What would be a one-line (and hopefully fast) way of getting the vector
[a;a;a;b;b;b;c;c;c]
for arbitrary n and m?
V=[ 1;2;3];
reshape(repmat(V',3,1),[],1)
ans =
1
1
1
2
2
2
3
3
3