need help about solving differential equation [closed] - matlab

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have tried to solve this following program in matlab but failed.
clear all
syms y(x)
y=dsolve(2.5e-3*diff(y, 2) + 0.5*diff(y) +122.5*y == 2570);
y=0 , y=20 ;
I want to find value of y.

Assuming this is not simply a copy-past error, you need to specify the location of the boundary values (or potentially initial conditions). The easiest way to do this is within the call to dsolve itself (since I don't know the locations, I'm going to assume y = 0 at x = 0 and y = 20 at x = 1/50):
syms x y(x)
xa = sym(0);
xb = sym('1/50');
y(x) = dsolve(2.5e-3*diff(y, 2) + 0.5*diff(y) + 122.5*y == 2570,y(xa)==0 , y(xb)==20)

Related

How to define a function with a matrix [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to define a two variables function g so that g(x,y) be a 2*2 matrix. To do this, I define g(x,y)=[1,1;x,y] but when I put g(1,1) I don't get any answer. How can I evaluate to g?
The code g(x,y)=[1,1;x,y] itself will not do anything. I assume that your expect result will be g=[1,1,1,1]? Therefore you should do as follow:
g=g_func(1,1);
disp(g)
function g=g_func(x,y)
g=[1,1;x,y];
end
It's not that different from the previous answer, but perhaps an anonymous function would meet your needs:
>> g = #(x,y)[1,1;x,y];
>> g(5,6)
ans =
1 1
5 6
Alternatively, if you want g to accept only one input (i.e. a 2-element vector, instead of two scalars), you could do:
g = #(x)[1,1;x(1),x(2)];
% or
g = #(x)[1,1;x(:).'];

Derivative of a function in Matlab [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I tried to take the derivative the function, but I can't find my mistake:
syms x
A = -1.6*x^2+18.7*x+3.4
This returns (187*x)/10 - (8*x^2)/5 + 17/5.
Then, diff(A) yields 187/10 - (16*x)/5.
There is no mistake here. The derivative of a second degree polynomial is a first degree polynomial... hence the variable x is still present in the result and you cannot evaluate it numerically unless you give a value to x:
vpa(subs(diff(A),x,4)) % evaluates the derivative for X=4, yields 5.9
If you want to reduce your function to a scalar value, a second order derivative must be taken:
vpa(diff(A,2)) % this returns: -3.2
Finally, if you just feel that the numerical parts of the result are "messy" and should be evaluated, you can call the vpa function on the derivative:
vpa(diff(A)) % this returns: 18.7 - 3.2*x

How do I integrate a vector in MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to perform integration on a vector L but I don't know exactly what to use. I want to obtain a (the integral) as a vector that is the same size as NT.
clc;clear;
syms x
NT=input('NT=');
L=zeros(NT,1);
for i=1:NT
disp('Longeur de travée')
L(i)=input('L = ');
L(i)=L(i);
fa(i)=L(i).*x^2;
a(i)=int(fa)
end
An easy way would be to use trapz. If you have X and Y such that Y(i) = f(X(i)) (so Y contains the values of some function at the location X) then you simply do
I = trapz(X, Y)
In your case, you can do
I = trapz(L, fa)
I guess, looking at your code.
Note that you could use more advanced techniques, that will, in principle, give you a better result (because they are higher-order). This is just one method, but an easy one.

Matlab: How do you implement this sum? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to implement the following:
x_i = e ^ (-1 - sum (y_j * A_ji))
where i = 1..10, j = 1..5 and A is a 5x10 matrix (randomly generated).
I tried using symsum but it gave me an index error. Could someone please help me figure out how to implement this?
With
A = rand(5,10); %# random 5x10 array
y = rand(1,5); %# random 1x5 array
Your sum becomes
x = exp( -1 - y*A);
thanks to linear algebra.

Error in MATLAB's plot function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this function
h45 = #(x) 1 / (1 + exp(-thDesnorm' * [1 45 x]'));
where thDesnorm is:
[-23.6608
0.1919
0.1866]
When i want to plot this function this way:
domain = 0:1:100;
figure;
plot(domain, h45(domain));
I get this error:
Error using *
Inner matrix dimensions must agree.
Error in #(x)1/(1+exp(-thDesnorm'*[1,45,x]'))
This function works when calling it with, for example, h45(1).
I guess than in plotting, the function is receiving all the domain vector as the parameter x, and not one by one the values of this vector.
As you guessed, the parameter x is your full array domain. This obviously causes a dimensional error in the dot product thDesnorm'*[1,45,x]. A quick fix would be using arrayfun to evaluate h45. For instance:
thDesnorm = [-23.6608
0.1919
0.1866];
h45 = #(x) 1 / (1 + exp(-thDesnorm' * [1 45 x]'));
domain = 0:1:100;
figure;
plot(domain, arrayfun(#(x)h45(x),domain)) % See modification in h45 call