I am trying to execute the code written below and getting the error given below, help me to fix this error?

The following error occurred converting from sym to double: Error using mupadmex Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in ExampleLoop (line 13) h(i,j) = 10*(((t(i,j)-.1)/.4)^-0.5-1)^.5;
code: clc; clear all; close all;
t = zeros(100,48); t(1,:) = .5;
t(:,1) = 0;
d = zeros(100,48);
k = zeros(100,48);
h = zeros(100,48);
syms t(i,j);
h(i,j) = 10*(((t(i,j)-.1)/.4)^-0.5-1)^.5;
k(i,j) = 10^-8*(t(i,j)-.1)/(.4)*(1-(1-((t(i,j)-.1)/0.4)^2)^.5)^2;
dh_dt(i,j) = diff(h(i,j));
dk_dt(i,j) = diff(k(i,j));
c(i,j) = 1/(dh_dt(i,j));
d(i,j) = k(i,j)/c(i,j);
dd_dt(i,j) = diff(c(i,j));
for i=2:99;
for j=1:47;
a = (1-.5*d(i,j)-.5*d(i+1,j));
b = .5*d(i,j);
c = .5*d(i+1,j);
t(i,j+1) = a*t(i,j)+b*t(i-1,j)+c*t(i+1,j)+.5*(k(i+1,j)-k(i-1,j));
end
end

 Akzeptierte Antwort

There are a few things wrong that I can see:
Do not use the ‘syms’ statement. You don’t need it. Use nested for loops for ‘i’ and ‘j’ instead.
In this and other statements like it:
dh_dt(i,j) = diff(h(i,j));
you are taking the difference of a single scalar element. That is not possible. (This would not have worked as a symbolic either, because your h matrix was not created as a function of a symbolic variable.)
What do you want to do in your code?

8 Kommentare

Well, yes I considered, what you said. can you tell me how to differentiate a function w.r.t. t , which is actually a matrix. I am trying to solve a non linear PDE here. How can I do it only in one script?
Please post your nonlinear PDE and we can work on it. I cannot figure out what you are doing with your code. I have the MATLAB PDE Toolbox, so if you have it as well, your equation should be possible to integrate. (I have not needed to use the PDE Toolbox in a while, so will have to re-learn it, but it should be possible to integrate your PDE.)
Thanks for your response. But actually I have to solve this equation using numerical techniques and writing code for it. I have posted the problem in the image
Here D(theta) is K(theta)*dh/d(theta).
This now seems to be a homework assignment. We at MATLAB Answers usually don’t solve problems we know or suspect are homework, but will do our best to help with MATLAB coding problems.
You must have been given an algorithm to code in order to solve this. Post the algorithm (we need to know what you want to do), do your best to implement it, and we will help you if you have problems making your code work. If you need to find an algorithm, the MIT site Numerical Methods for Partial Differential Equations and the Wolfram site Numerical Solution of Partial Differential Equations might both be helpful.
I know the algorithm. But I am having problem implementing in Matlab. Let me explain the algorithm: 1. I have to get theta as a function of x and t. 2. D and K are dependent on theta. 3. Now to solve the problem I will use discret form of above pde.
theta(i,j+1) = a*theta(i,j) + b*theta(i-1,j) + c*theta(i+1,j) + (1/1440)*(k(i+1,j) - k(i-1,j));
a = (1-(1/1440)*d(i,j)-(1/1440)*d(i+1,j));
b = (1/1440)*d(i,j);
c = (1/1440)*d(i+1,j);
where i and j are space and time coordinates respectively.
4. as evident in the above equation theta(i,j+1) is function of theta(i,j),theta(i+1,j),theta(i-1,j). By boundary and initial conditions I know theta(1,;) and theta(:,1). So above equation will give value for all possible i ans j's.
I hope algorithm is clear now.
*My problem exactly is:
I Know 'h' as a function of theta and 'd' = k*(derivative of h w.r.t. theta). I differentiated the h as follows:-
syms theta;
h = a function of theta;
diff(h);
*but this way dh/d(theta) is stored as a function of theta, while actually theta is a matrix, i don't know how to give a specific matrix element as input in this case.* _
I do not know what theta is, but the usual way to calculate numerical derivatives is something like this:
t = linspace(0,2*pi);
f = sin(t);
dfdt = diff([0 f]) ./ diff([0 t]);
figure(1)
subplot(2,1,1)
plot(t, f)
title('sin(t)')
grid
subplot(2,1,2)
plot(t, dfdt)
title('df/dt')
grid
I suggest you not use the symbolic derivative, but calculate the derivative similarly to the way I calculated the derivative in my example here. You would not give a specific matrix element of theta as input, but the difference in either the ‘i’ or ‘j’ dimension between two adjacent elements of theta, depending on whether you were differentiating it w.r.t. x or t.
Thank you, that was very helpful.
My pleasure!
Apologise for the delay — GMT-6 hours here.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by