Writing mathematical function in code
显示 更早的评论
Hi! Can somebody help me how to write the following function in Matlab?
(1/2*N)*||x||^2 + (1/2*N) Σ max(0.1-(v(i)*(x0+u(i)*x))^2) with i from 1 up to N
I thought to start from the max but i only know how to calculate the max of a matrix. Should i put the prices of (0.1-(v(i)*(x0+u(i)*x))^2) in a matrix and then find the max?
Thank you.
3 个评论
Walter Roberson
2019-11-28
Should i put the prices of (0.1-(v(i)*(x0+u(i)*x))^2) in a matrix and then find the max?
Possibly. You could vectorize,
(0.1-(v.*(x0+u.*x)).^2)
however it is not clear to me what max() is being taken with respect of. Is x possibly a vector?
If x is a vector then
max(0.1-(v(:).*(x0(:).'+u(:).*x(:).')).^2, 2)
would be the maximum over the x, in vectorized form. This requires R2016b or later to work as written.
The (:).' there are forcing whatever shape x0 and x are, to become row vectors. If you know for sure that x0 and x are row vectors then you can remove the (:).' operations (but you still need the (:) on v and u)
sissy sissy
2019-11-28
Walter Roberson
2019-11-28
max(0.1-(v(:).*(x0+u(:).*x(:).')).^2, 2)
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Surfaces, Volumes, and Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!