Error in using Inline function and fzero

Hello, Can anybody help me? I want to figure out where I am doing a mistake in program. The only variable is X and Cp is an input.
Cp = double(zeros(49792,1));
P = size(x.Time_Series);
Q = P(1,1);
Cp(1,1)= 0.126826003999670;
Cp(2,1)= 0.325943036562508;
for k=3:Q;
k
hx = inline( '12.175*(X - 2*Cp(k-1,1) + Cp(k-2,1)) + 13.525*((X - Cp(k-1,1))* abs(X - Cp(k-1,1))) + X - SINGLE_FINAL_TIME_SERIES_OF_TAPS_NEAR_DOMINANT_OPENING(k,1)','X',Cp);
hp = fzero (hx,0,Cp,SINGLE_FINAL_TIME_SERIES_OF_TAPS_NEAR_DOMINANT_OPENING(k,1));
Cp(k) = hp(1);
end
I am getting this error
Error: The expression to the left of the equals sign is not a valid target for an
assignment.
If in inline function, I remove Cp which is given after X (at the very last near the bracket), then it shows error: too many inputs to inline function. Pls help. thanks.

 Accepted Answer

It doesn’t like the ‘==’ in SINGLE_FINAL_TIME_SERIES_OF_TAPS_NEAR_DOMINANT_OPENING(k,1)==0.
Unlike anonymous functions that use workspace variables, inline functions ignore anything that isn’t in their argument list. It has no idea what SINGLE_FINAL_TIME_SERIES_OF_TAPS_NEAR_DOMINANT_OPENING(k,1) or k are.
Also, fzero only accepts single-variable functions.

9 Comments

Instead of using inline,fzero or anonymous function, I have used 'solve' to get the required solutions. My code is given below. The problem here is the program has become very slow. It is taking around 10 hours to solve get 49790 values. SINGLE_FINAL_TIME_SERIES_OF_TAPS_NEAR_DOMINANT_OPENING(k,1) is the input array of 49792*1 values. The value of X (stored as VD(1))is found out every time and this value together with the value from SINGLE_FINAL_TIME_SERIES... becomes the input for the next equation. Is there any way to make the program faster? My Code:
tic
Cp(1,1)= 0.126931068442331;
Cp(2,1)= 0.326161603224261;
syms X;
for k=3:Q;
k
VD(1)= solve(12.175 *(X - 2*Cp(k-1,1) + Cp(k-2,1)) + 13.525*(X-Cp(k-1,1)) * abs(X-Cp(k-1,1)) + X- SINGLE_FINAL_TIME_SERIES_OF_TAPS_NEAR_DOMINANT_OPENING(k,1)==0,X);
Cp(k)= VD(1);
end
toc
Thanks.
For a start, I suggest that you explore the fsolve function rather than using the Symbolic Math Toolbox. (It’s not designed to do those kinds of calculations rapidly.)
Are X and SINGLE_FINAL_TIME_SERIES_OF_TAPS_NEAR_DOMINANT_OPENING(k) single values, vectors, or matrices?
Ok. I will start doing what you said. X and Single_final... are singles values stored in a matrix of size 49792*1. Single_final_... is already known and is matrix of 49792*1.
Post about 15 values for SINGLE_FINAL_ ... and I’ll give a go at solving for X numerically.
I’m still not following what you want to do, though. I get the impression that you want to start with initial estimates for Cp, solve for X, and replace it in the next value for Cp, then continue through the end of SINGLE_FINAL_ ... Is that close to reality?
Okay, if I was not clear, then I am explaining it to you again. I first manually or used solve 2 times to find the first 2 values of Cp. For example, when X was found out in first step, then it was used as Cp input in the 2nd time step. Also I am using the values from Single_time.... as my input. And this way it is repeated till the end. I think what you have written above is right. I ran my program by using SOLVE to get around 10000 values of Cp but the values are not correct in between. Here are first 20 values of Single_time__ 1.88848000000000 1.74232000000000 1.54512000000000 1.46624000000000 1.20408000000000 0.874640000000000 0.570720000000000 1.13216000000000 1.31544000000000 1.43608000000000 1.63328000000000 1.42912000000000 1.22032000000000 1.49640000000000 1.63328000000000 1.71680000000000 1.51728000000000 0.965120000000000 0.468640000000000 0.740080000000000
Does this produce the result you want? The plot looks strange, but then I don’t know what you’re researching. (I plotted it to see if it looked reasonable.)
SINGLE_FINAL = [1.88848000000000 1.74232000000000 1.54512000000000 1.46624000000000 1.20408000000000 0.874640000000000 0.570720000000000 1.13216000000000 1.31544000000000 1.43608000000000 1.63328000000000 1.42912000000000 1.22032000000000 1.49640000000000 1.63328000000000 1.71680000000000 1.51728000000000 0.965120000000000 0.468640000000000 0.740080000000000];
Cp(1)= 0.126826003999670;
Cp(2)= 0.325943036562508;
% hx = inline( '12.175*(X - 2*Cp(k-1,1) + Cp(k-2,1)) + 13.525*((X - Cp(k-1,1))* abs(X - Cp(k-1,1))) + X - SINGLE_FINAL_TIME_SERIES_OF_TAPS_NEAR_DOMINANT_OPENING(k,1)','X',Cp);
% CALLING ‘f1’:
% Define cp = [Cp(k-1); Cp(k-2)]
% f1(x,cp,SF(k))
f1 = @(X,cp,SFTSOTNDO) 12.175 .* (X - 2 .* cp(1) + cp(2)) + 13.525 .* ((X - cp(1)) .* abs(X - cp(1))) + X - SFTSOTNDO;
for k = 3:length(SINGLE_FINAL)
cp = [Cp(k-1); Cp(k-2)];
sf = SINGLE_FINAL(k);
X = fzero(@(x)(f1(x,cp,sf)),1);
Cp(k) = X;
end
figure(1)
plot(SINGLE_FINAL, Cp)
grid
xlabel('SINGLE FINAL')
ylabel('Cp')
I kept your inline function in for reference, but commented it out.
I also vectorised f1 because that generally avoids problems.
Eureka! It does produce the results very fast. Unbelievably in 163 seconds for all 49792 values. Thanks a lot for this. I want to explain you what I am trying to achieve here although it may not be relevant to your field. Cp means pressure coefficient. Pr. coefficients are measured in a boundary layer wind tunnel at frequency of 500 Hz for 100 s. That is why there are 50000 values approx represented by SFTSOTNDO. The values measured were external Cp and I wanted to derive internal Cp using the equation above. There is no need of plot between these two. However, the values I have got are relatively higher. They should all be less than 3 or 4. But leaving that, it is major work done for me. Thanks a lot again. I have named this program Star_Strider as a tribute and a sign of respect.
My pleasure!
Wow! And a tribute, too! Thank you!
I have a background in physical chemistry and am an instrument-rated private pilot, so I'm slightly familiar with what you are doing. I do not have sufficient background in fluid dynamics to understand it in any detail, though.
Thanks for helping and telling about your background. Take care.

Sign in to comment.

More Answers (1)

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!