Can anypody help with this piece of code?
The problem is with the use of the integeral function
%------------------------------------------------------------------------%
clear variables;
close all;
clc;
%------------------------------------------------------------------------%
x=-1:0.1:1;
n=length(x);
y=zeros(1,n);
for i=1:n
y = m_cost(x);
end
plot(y)
and here is my function:
function y =m_cost(x)
fun1=inline('0-x','x');
fun2=inline('x-4','x');
syms fun1;
syms fun2;
syms x;
if (x>=0)
y=x.^2+4*x+8*int(fun1,x,0,10);
else
y=x.^2+4*x+8*int(fun2,x,0,10);
end

댓글 수: 5

can you type the equation of integral u trying to calcuate?
darova
darova 2020년 4월 24일
What about this?
Well I dont know what inline does, I want you to post the math equation so I can try to solve it with different method if u want
Is it a differential equation?
Thank you. This is really informative.
If I have this cost function, for example, is using the integeral function the right option? I want to integerat int(p) over time dt (axis x).
% Assume this is the results of the temperature function v(t)=v(i-1)+A*(time);
v = -1:0.1:5;
% A is the heaating parameter
A = 4/3;
% Cost at each v(t) is given by the expression:
% y(v(t))=(50*int(p)+10*x+30)/(x/A);
% P = x-4 if x > 4 and x <= 5
% p = 0 if x >= 0 and x <= 4
% p = -x if x >= -1 and x < 0
y = cost(v)
function y= cost(v)
func1 = @(v) v-4;
func2 = @(v) -v;
% Cost of each v(t) at time t is given by the expression:
% y(v(t))=(50*int(p)+10*x+30)/(x/A);
% P = x-4 if x > 4 and x <= 5
% p = 0 if x >= 0 and x <= 4
% p = -x if x >= -1 and x < 0
if (v > 4 & v <= 5)
y = ((50*integeral(func1,0,inf)+((v./A)*10)+30)/(v./A);
elseif ( v>=0 & v<=4)
y = (((v./A)*10)+30)/(v.A);
else
y = ((integeral(func2,0,inf))+((v./A)*10)+30)/(v./A);
end
end

댓글을 달려면 로그인하십시오.

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 24일

0 개 추천

There is no need to use inline. You can use an anonymous function. Also, int() is used for symbolic integration. Here you can use numeric integration using integral(). The function can be vectorized so there is no need to use a for-loop
%------------------------------------------------------------------------%
clear variables;
close all;
clc;
%------------------------------------------------------------------------%
x = -1:0.1:1;
n = length(x);
y = m_cost(x);
function y =m_cost(x)
fun= @(x) -x;
y = x.^2+4*x+8*integral(fun,0,10);
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 4월 24일

1 개 추천

int is only for use with Symbolic functions or symbolic expressions. inline objects cannot be used with int.
You should only use inline if you have a very specific reason to use it. You should be using symbolic or anonymous functions instead.
However your
syms fun
on the next line overwrites fun the same as if you had done
fun = sym('fun') ;
So when you int that it is just a symbolic constant and the integral is the constant times the difference in time. Your function is going to return a symbolic expression involving the two unresolved values x and fun. x is unresolved because the
syms x
that you did overwrote the x parameter passed to the function.
so you return a symbolic expression that contains unresolved variables and outside the function you assign that into part of y. But y is double precision and you cannot store a symbolic expression with unresolved variables in double precision.

카테고리

질문:

2020년 4월 24일

댓글:

2020년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by