Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Subscripted assignment dimension mismatch.

조회 수: 3 (최근 30일)
Ben Johannesson
Ben Johannesson 2016년 2월 14일
마감: MATLAB Answer Bot 2021년 8월 20일
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

답변 (1개)

Walter Roberson
Walter Roberson 2016년 2월 14일
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
Ben Johannesson
Ben Johannesson 2016년 2월 15일
How did you simplify Ip? the solution should not be that simple
Walter Roberson
Walter Roberson 2016년 2월 15일
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)

이 질문은 마감되었습니다.

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by