Info

This question is closed. Reopen it to edit or answer.

Subscripted assignment dimension mismatch.

3 views (last 30 days)
Ben Johannesson
Ben Johannesson on 14 Feb 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
I am writing a code to solve for the variable t at various values of x in the expression bellow. k,h, and c are previously know constants. x in an input I have determined from elsewhere. diffD are the known values I am equating the expression to. The length of RHS, x2, and diffD are all 7
Ip = @(x,t) -(((5*k*t*x)*exp((c*h)/(k*t*x)))-(5*k*t*x))/((k*t*x^2)*(exp((c*h)/(k*t*x))-1));
syms t;
for i=1:length(x2)
RHS(i)=Ip(x2(i),t);
end
for i=1:length(x2)
eqn = RHS(i) == diffD(i);
td(i) = solve(eqn,t);
end

Answers (1)

Walter Roberson
Walter Roberson on 14 Feb 2016
Ip(x2(i),t) simplifies to -5/x2(i) which is independent of t. When you solve() for -5/x2(i)==RHS(i) for t, you get no solutions, which is length 0. You cannot write a solution of length 0 into something of length 1, td(i)
Remember, solve() is not constrained to return a single value: it can return 0, 1, or several values. Therefore unless you have proven that there is a unique solution (and that solve() is good enough to find it) you should never write the results of solve() directly into any location of fixed length.
  2 Comments
Ben Johannesson
Ben Johannesson on 15 Feb 2016
How did you simplify Ip? the solution should not be that simple
Walter Roberson
Walter Roberson on 15 Feb 2016
Look at your numerator:
-(5*k*t*x*exp(c*h/(k*t*x)) - 5*k*t*x)
clearly the 5*k*t*x factors for the terms, giving
-(5*k*t*x*( exp(c*h/(k*t*x)) - 1 ))
and the denominator has (k*t*x^2) which has k*t*x in it, so that cancels from the top and bottom leaving
-5*(exp(c*h/(k*t*x)) - 1) / (x*(exp((c*h)/(k*t*x)) - 1))
and we easily see the exp() term cancels. This gives us a net result of -5/x
Perhaps you did not want the first set of brackets that distributes the leading negative across the entire numerator: perhaps you wanted
-5*k*t*x*exp(c*h/(k*t*x)) - 5*k*t*x
That would still have the k*t*x cancel from the numerator and denominator but the (exp(c*h/(k*t*x))-1) would not cancel because in the numerator it would effectively be -5*(exp(c*h/(k*t*x))+1) with +1 instead of -1. The result would be something that could be solved for t, giving
t = c .* h ./ (k .* x2 .* ln((RHS .* x2 - 5) ./ (RHS .* x2 + 5)))
as the general solution in vectorized form (provided that RHS and x2 are the same size and shape)

This question is closed.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!